When I try to determine end of file with function feof(FILE *)
, I find it does not work as I expected: an extra read is required even if the stream does end. e.
Do not use feof() or any variants - it is as simple as that. You want it to somehow predict the next read will fail, but that's not what it does - it tells you what the result of the PREVIOUS read was. The correct way to read a file is (in pseudocode):
while( read( file, buffer ) ) {
do something with buffer
}
In other words, you need to test the result of the read operation. This is true for both C streams and C++ iostreams.