Jul 17, 2010 at 6:44am
eof does not get set until you are at eof AND you have attempted to read something. Don't loop on eof, instead loop on the stream itself:
while(newfile)
Jul 17, 2010 at 6:50am
Because
eof is only set when you try to read beyond the end of file - which means you
are in the middle of the loop when this happens, which means
1 2
|
newfile >> x;
newfile >> y;
|
fails, which means x and y don't change from their previous values, and
1 2
|
cout << x << endl;
cout << y << endl;
|
will display the previous values. Then you go back to the top of the loop and the eof test fails - but it is too late then.
This
repeat-of-last-value-in-file is a problem when you loop on
eof like you have done.
It also has another problem, in that if the file is bad or problems reading values - you can get an infinite loop situation.
Last edited on Jul 17, 2010 at 6:51am