I need some clarification on the eof( ) file stream function in C++. In my textbook, it says "inStream.eof() does not become true until the program attempts to read one character beyond the end of the file." if inStream were an ifstream object. However, when I've been writing a program that reads integers from a file, and let's say my ifstream object was called "fin", then fin.eof() gets set when I read the last integer. Therefore, if I had some mini program that simply read integers from one file and printed them to another file:
ifstream fin("input.txt");
ofstream fout("output.txt");
int num;
fin >> num;
while (!fin.eof()) {
fout << num << endl;
fin >> num;
}
and lets say my input file was
9
10
11
Then my output would be
9
10
By debugging I found that for some reason when "fin >> num" reads the last number, it sets the eof flag so the condition in the while loop evaluates as false and it breaks. This is contrary to what my textbook said and I'm just confused. If it's compiler dependent, then I'm using visual studio 2019 so maybe that's the issue. Else, I'm not sure since my textbook is saying one thing and a different thing is happening in my program.
Off the top of my head, the only thing I can think of is my textbook explained eof() in terms of reading characters and right now I'm reading integers from a file so maybe there's a difference.