I have an issue with some code I have been working on. I am trying to read the contents of a text file (input.txt) into a variable fileContents. The loop in the code enters, but the program produces no output. Some of the variables are not used, I know about this. What is wrong?
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int main(int argc, char const *argv[])
{
ifstream input("input.txt");
ofstream output("output.txt"); //init output controller
// new lines will be skipped unless we stop it from happening:
//input.unsetf(std::ios_base::skipws);
// count the newlines with an algorithm specialized for counting:
unsigned line_count = std::count(std::istream_iterator<char>(input),std::istream_iterator<char>(), '\n');
string fileContents = ""; //init message, that will be filled by input.txt
string str; //temp string
while (input >> fileContents)
{
//cout << "loop entered";
cout << fileContents << "\n";
}
//cout << "test" << "\n";
return 0;
}
Related
I am new to passing values to functions, please guide me what I am doing wrong here, thanks!
The question: Write a C++ program in which, read a c-string sentence
one by one from a file “sentence .txt”. Now your task is to break each
word of sentence into another c-string word, now write that word into
a file “word.txt”. Note : You must create atleast 1 function to
separate the words from sentence, you cannot use strings.
#include <iostream>
#include <fstream>
using namespace std;
char sentence2word(char array[100])
{
ofstream fout2;
fout2.open("word.txt");
fout2 << array << endl;
return array[100];
}
int main()
{
ifstream fin;
fin.open("sentence.txt");
char array[100];
fin >> array;
cout << "Output successful!";
sentence2word(array);
return 0;
system("pause");
}
The following program show how to get started with reading and writing from/into text files in C++. This is just to get you started and in practice i use std::string and std::istringstream to do this but in your note it is written that we cannot use strings so i did not use std::string. The program reads line by line from an input.txt file and write word by word into an output.txt file.
#include <iostream>
#include <fstream>//needed to read/write files
#define MAX_NUMBER_OF_CHARACTERS 500
using namespace std;
//function that writes lines word by word into output.txt
void writeWordByWord(std::ofstream &m_outFile, char (&lineArg)[MAX_NUMBER_OF_CHARACTERS])
{ int i = 0;
while(lineArg[i] != '\0')
{
if(lineArg[i] != ' ')
{m_outFile << lineArg[i];
//std::cout<< lineArg[i]<<" wrote"<<std::endl;
++i;
}
else{
m_outFile << '\n';
++i;
}
}
//m_outFile << '\n';
}
int main()
{
cout << "Hello World" << endl;
char line[MAX_NUMBER_OF_CHARACTERS];
std::ifstream inFile("input.txt");
std::ofstream outFile("output.txt");
while(inFile.getline(line, MAX_NUMBER_OF_CHARACTERS, '\n'))
{
std::cout<<line<<std::endl;
writeWordByWord(outFile, line);
}
inFile.close();
outFile.close();
return 0;
}
This is my code.
I am supposed to count the number of 'duck' in a txt file and print string like "There were 2 ducks in animals01.txt"
Now I get no error and nothing return.
Please tell me what's wrong?
#include <iostream> // for printf()
#include <cstdlib> // for exit(), perror()
#include <fstream> // for ifstream
using namespace std;
int main(int argc, char *argv[])
{
if (argc!=2) {
// if argc is not 2, print an error message and exit
cerr << "Usage: "<< argv[0] << " inputFile" << endl;
exit(1); // defined in cstdlib
}
return 0;
int num = 0;
ifstream ifs;
ifs.open(argv[1]);
string line;
do{
getline(ifs, line);
cout<<line<<endl;
if(line == "duck"){num++;}
}while(!ifs.eof());
cout<<"There were"<<num<<"ducks in"<<argv[1]<< endl;
}
You have the line return 0; before you actually did anything, and when you return main() the program is terminated.
By the way, don't use while(!ifs.eof()) because the eof flag only gets set at the first attempt to read past the end of the file, not when you read exactly to the end of the file due to a line break at the end. Do something like this. Also, fix your indenting as it is very misleading.
Read the file word by word.
Example:
#include <string>
#include <vector>
#include <fstream>
#include <iostream>
using namespace std;
int main()
{
string str, strToSearch = "ducks";
int wordCount = 0;
ifstream fin("thisfile.txt");
while (fin >> str) // Will read up to eof() and stop at every whitespace it hits. (like spaces!)
{
if(str==strToSearch)
wordCount++;
}
fin.close();
cout<<"There were"<<wordCount<<"ducks in thisfile.txt"<<endl;
return 0;
}
So Lets say this is what the input file contains
12
Hello
45
54
100
Cheese
23
How would I print it out on the screen in that order.
This is what I had but it skips some lines.
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
int number;
string word;
int loop = 0;
ifstream infile;
infile.open("arraynumbers.txt");
while(infile >> number >> word)
{
if( infile >> number)
{
cout << number << endl;
}
if(infile >> word)
{
cout << word << endl;
}
}
return 0;
}
I suggest using www.cplusplus.com to answer these questions.
However, you are on the right track. Since you are just outputting the contents of the file to stdout, I suggest using readline() and a string. If you need to access the numeric strings as ints, use the atoi() function.
Example:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
string line;
ifstream file("arraynumber.txt");
if (file.is_open()) {
while (getline(file, line)) {
cout << line << endl;
}
file.close();
} else cout << "Error opening arraynumber.txt: File not found in current directory\n";
return 0;
I' m training with file in C++. I have a problem: If I write more than 1 word in the terminal, in the file I have just the first written word.
Example
Hello devs
in the file I will have only
Hello
and the next time, that I'll execute the programme the new word will be in the next row.
Code
#include <cstdlib>
#include <iostream>
#include <fstream>
using namespace std;
/*
*
*/
main(int argc, char** argv) {
string test;
string st;
ofstream biblio;
biblio.open("lis.txt", ios::app);
if(biblio.is_open()){
cout <<" Write in file: ";
cin >> test;
biblio << test << "\n";
biblio.close();
cout<<"I'm in the if!!!\n";
}
ifstream biblio1;
biblio1.open("list.txt");
if(biblio1.is_open()){
while(getline(biblio1,st)){
cout << st << '\n';
}
cout<<"I'm in the other if!!!\n";
biblio1.close();
}
else{
cout<<"Not possible to open the file\n";
}
}
operator >> uses white space to delineate items, use std::getline(std::cin, test) instead.
I'm trying to get the beginning address of each line of my file as I read it, and print it out to the screen, but for some reason it just results in an endless loop. The file i'm reading is just a normal text file. Here's what I have going right now.
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
using namespace std;
int main(int argc, char* argv){
ifstream file;
string name, lnstr;
int addy;
if (argc > 1)
name = argv[1];
else
{
cout << "Please Enter Your Filename: ";
getline(cin, name);
}
file.open(name.data());
if(!file)
{
perror(name.data());
exit(1);
}
addy = 0;
while(getline(file, lnstr))
{
cout << file.seekg(addy, ios::beg) << endl;
addy++;
}
}
Even if I put 0 as the first parameter of seekg, it still results in an endless loop, or it just shows the same number a bunch of times. Not sure what i'm missing.
When you call ios::beg you set the position of the get pointer to the beginning of the file. You don't actually need this call and this code should work for you:
file.open(name.c_str()); // open file
if(file) {
while(getline(file, lnstr)) {
cout<< lnstr <<endl;
}
}
More on seekg.
I think you want tellg, not seekg.