|the content of the file "zhouting.txt" is "zhouting like lichao!"|
I tested the program with exactly that file content. This was the output:
but ... if I press enter at the end of the line, the file content becomes
"zhouting like lichao!\n"
and the output is now:
That is only part of the problem. The behaviour is inconsistent depending on presence of whitespace. When the last item is read, because there is whitespace afterwards, end of file is not reached. But on the next iteration, the read will fail, so it just prints out the existing contents of the variable
The other part of the problem is we don't even know whether the file is being read at all.
Let's change the code to this, but keep the same input file:
using namespace std;
istream &iostream_input(istream &is)
// string str;
int str = 0; // use an integer instead of a string
is >> str;
cout << str << endl;
The output is now
... repeat to infinity
The solution to both problems is the same.
Check that the file was actually read before trying to proceed.
at the start of the loop tells you about what happened in the past
. It does not predict whether or not the next
read attempt will be successful. In addition not all conditions will even set eof() at all. If there is an error,
be true and it will loop forever.
The easiest way to do that is to put the input operation inside the loop condition (and forget you ever heard of eof()*
... at least for now).
*There are valid uses for eof() but they may be considered advanced /specialised applications.