https://gcc.gnu.org/bugzilla/show_bug.cgi?id=69218
--- Comment #4 from Dmitry Igrishin <dmitigr at gmail dot com> --- (In reply to Jonathan Wakely from comment #2) > This is the correct behaviour required by the C++ standard, your assertion > is incorrect, that is not guaranteed to always be true. > > When reading the last word of the file (which in your test is "character") > the compiler keeps reading while there are non-whitespace characters. > Because there are no non-whitespace characters after the word "character" it > stops reading and sets eofbit, but doesn't set failbit because reading > succeeded. > > If you have a newline at the end then reading the word "character" does not > reach EOF, but on the next time round the loop it fails to read any > non-whitespace characters, so sets failbit. > > This is why you should write "while (in >> w)" instead of checking for EOF. And thank you for explanation, Jonathan!