I have just started learning file handeling in C++.
I know the basics, like fstream contains ofstream | ifstream, etc.
I tried to make a project, but there I needed a function that prints out all the numbers from a txt file.
You might thing that this code would work:
int gpr(const string& filename){
fstream file
file.open(filename, ios::in);
if(!file){
cout << "The program is unable to locate the file." << endl;
return -1;
}
int value;
file >> value;
file.close();
return value;
}
Which would, but the only problem is that it only reads the first line. Like if my txt file contains this:
90
102
91
The overall results would be 90. The other ones under 90 are getting ignored. I need a code that reads every single line and returns an integer. Please, make sure your code is:
1. Simple
2. Understandable
3. Not so long(in columns)
4. only contains these libraries: iostream, fstream, string. Nothing else.