Posted by: suike February 11, 2009
help needed computer geeks about coding!!
Login in to Rate this Post:     0       ?        
Hi,
I had the exact same question four years back.

eof returns true only after a read effort is made after end of file.
This code:
do { read; do other stuff} while ( !eof)
with this file:
1
2

Your code reads value 2, finishes do, checks to see if eof is true....which it is not right now, because you successfully read 2. The code goes back to do...tries to read something....doesn't have anything....the variable still holds the value of 2....it is ONLY THIS TIME eof is true....the while loop terminates.

There are two ways of getting around this:
1> as techguy suggested in the post above this: using while(inputstream>>stuff)
2> avoid do-while, use just while, and do
read; while(!inputstream.eof()){blah; read}

Gluck!



Read Full Discussion Thread for this article