There's nothing wrong with your code. fstream::eof() returns true whenever the stream has ever encountered end-of-file. Even if you open and close or seek the stream, ieof() will still return true if the stream has encountered end of file before. To clear it, you can call ifstream::clear().