I gave an answer which I wanted to check the validity of stream each time through a loop here.
My original code used good
and looked similar to this:
Using foo.good()
just tells you that the previous read operation worked just fine and that the next one might as well work. .good()
checks the state of the stream at a given point. It does not check if the end of the file is reached. Lets say something happened while the file was being read (network error, os error, ...) good will fail. That does not mean the end of the file was reached. Nevertheless .good() fails when end of file is reached because the stream is not able to read anymore.
On the other hand, .eof()
checks if the end of file was truly reached.
So, .good()
might fail while the end of file was not reached.
Hope this helps you understand why using .good()
to check end of file is a bad habit.