Categories
c++ c++-faq iostream

Why is iostream::eof inside a loop condition (i.e. `while (!stream.eof())`) considered wrong?

652

I just found a comment in this answer saying that using iostream::eof in a loop condition is “almost certainly wrong”. I generally use something like while(cin>>n) – which I guess implicitly checks for EOF.

Why is checking for eof explicitly using while (!cin.eof()) wrong?

How is it different from using scanf("...",...)!=EOF in C (which I often use with no problems)?

9

  • 24

    scanf(...) != EOF won’t work in C either, because scanf returns the number of fields successfully parsed and assigned. The correct condition is scanf(...) < n where n is the number of fields in the format string.

    – Ben Voigt

    Apr 5, 2012 at 16:50

  • 6

    @Ben Voigt, it will return a negative number (which EOF usually is defined as such) in case EOF is reached

    – Sebastian

    Nov 23, 2012 at 23:44

  • 19

    @SebastianGodelet: Actually, it will return EOF if end of file is encountered before the first field conversion (successful or not). If end-of-file is reached between fields, it will return the number of fields succcessfully converted and stored. Which makes comparison to EOF wrong.

    – Ben Voigt

    Nov 24, 2012 at 15:06


  • 1

    @SebastianGodelet: No, not really. He errs when he says that “past the loop there is no (easy) way to distinguish a proper input from an improper one”. In fact it’s as easy as checking .eof() after the loop exits.

    – Ben Voigt

    Nov 24, 2012 at 16:52

  • 3

    @Ben Yes, for this case (reading a simple int). But one can easily come up with a scenario where while(fail) loop terminates with both an actual failure and an eof. Think about if you require 3 ints per iteration (say you are reading an x-y-z point or something), but there is, erroneously, only two ints in the stream.

    – sly

    Nov 24, 2012 at 19:47

589

Because iostream::eof will only return true after reading the end of the stream. It does not indicate, that the next read will be the end of the stream.

Consider this (and assume then next read will be at the end of the stream):

while(!inStream.eof()){
  int data;
  // yay, not end of stream yet, now read ...
  inStream >> data;
  // oh crap, now we read the end and *only* now the eof bit will be set (as well as the fail bit)
  // do stuff with (now uninitialized) data
}

Against this:

int data;
while(inStream >> data){
  // when we land here, we can be sure that the read was successful.
  // if it wasn't, the returned stream from operator>> would be converted to false
  // and the loop wouldn't even be entered
  // do stuff with correctly initialized data (hopefully)
}

And on your second question: Because

if(scanf("...",...)!=EOF)

is the same as

if(!(inStream >> data).eof())

and not the same as

if(!inStream.eof())
    inFile >> data

15

  • 13

    Worth mentioning is that if (!(inStream >> data).eof()) doesn’t do anything useful either. Fallacy 1: It’ll not enter the condition if there was no whitespace after the last piece of data (last datum will not be processed). Fallacy 2: It will enter the condition even if reading data failed, as long as EOF was not reached (infinite loop, processing the same old data over and over again).

    – Tronic

    Jan 20, 2013 at 16:20

  • 4

    I think it’s worth pointing out that this answer is slightly misleading. When extracting ints or std::strings or similar, the EOF bit is set when you extract the one right before the end and the extraction hits the end. You do not need to read again. The reason it doesn’t get set when reading from files is because there’s an extra \n at the end. I’ve covered this in another answer. Reading chars is a different matter because it only extracts one at a time and doesn’t continue to hit the end.

    Apr 6, 2013 at 16:59


  • 86

    The main problem is that just because we haven’t reached the EOF, doesn’t mean the next read will succeed.

    Apr 6, 2013 at 17:03


  • 1

    @sftrabbit: all true but not very useful… even if there’s no trailing ‘\n’ it’s reasonable to want other trailing whitespace to be handled consistently with other whitespace throughout the file (i.e. skipped). Further, a subtle consequence of “when you extract the one right before” is that while (!eof()) won’t “work” on ints or std::strings when the input is totally empty, so even knowing there’s no trailing \n care is needed.

    Apr 23, 2013 at 3:34

  • 2

    @TonyD Totally agree. The reason I’m saying it is because I think most people when they read this and similar answers will think that if the stream contains "Hello" (no trailing whitespace or \n) and a std::string is extracted, it will extract the letters from H to o, stop extracting, and then not set the EOF bit. In fact, it would set the EOF bit because it was the EOF that stopped the extraction. Just hoping to clear that up for people.

    Apr 23, 2013 at 8:23

107

Bottom-line top: With proper handling of white-space, the following is how eof can be used (and even, be more reliable than fail() for error checking):

while( !(in>>std::ws).eof() ) {  
   int data;
   in >> data;
   if ( in.fail() ) /* handle with break or throw */; 
   // now use data
}    

(Thanks Tony D for the suggestion to highlight the answer. See his comment below for an example to why this is more robust.)


The main argument against using eof() seems to be missing an important subtlety about the role of white space. My proposition is that, checking eof() explicitly is not only not “always wrong” — which seems to be an overriding opinion in this and similar SO threads –, but with proper handling of white-space, it provides for a cleaner and more reliable error handling, and is the always correct solution (although, not necessarily the tersest).

To summarize what is being suggested as the “proper” termination and read order is the following:

int data;
while(in >> data) {  /* ... */ }

// which is equivalent to 
while( !(in >> data).fail() )  {  /* ... */ }

The failure due to read attempt beyond eof is taken as the termination condition. This means is that there is no easy way to distinguish between a successful stream and one that really fails for reasons other than eof. Take the following streams:

  • 1 2 3 4 5<eof>
  • 1 2 a 3 4 5<eof>
  • a<eof>

while(in>>data) terminates with a set failbit for all three input. In the first and third, eofbit is also set. So past the loop one needs very ugly extra logic to distinguish a proper input (1st) from improper ones (2nd and 3rd).

Whereas, take the following:

while( !in.eof() ) 
{  
   int data;
   in >> data;
   if ( in.fail() ) /* handle with break or throw */; 
   // now use data
}    

Here, in.fail() verifies that as long as there is something to read, it is the correct one. It’s purpose is not a mere while-loop terminator.

So far so good, but what happens if there is trailing space in the stream — what sounds like the major concern against eof() as terminator?

We don’t need to surrender our error handling; just eat up the white-space:

while( !in.eof() ) 
{  
   int data;
   in >> data >> ws; // eat whitespace with std::ws
   if ( in.fail() ) /* handle with break or throw */; 
   // now use data
}

std::ws skips any potential (zero or more) trailing space in the stream while setting the eofbit, and not the failbit. So, in.fail() works as expected, as long as there is at least one data to read. If all-blank streams are also acceptable, then the correct form is:

while( !(in>>ws).eof() ) 
{  
   int data;
   in >> data; 
   if ( in.fail() ) /* handle with break or throw */; 
   /* this will never fire if the eof is reached cleanly */
   // now use data
}

Summary: A properly constructed while(!eof) is not only possible and not wrong, but allows data to be localized within scope, and provides a cleaner separation of error checking from business as usual. That being said, while(!fail) is inarguably a more common and terse idiom, and may be preferred in simple (single data per read type of) scenarios.

5

  • 6

    So past the loop there is no (easy) way to distinguish a proper input from an improper one.” Except that in one case both eofbit and failbit are set, in the other only failbit is set. You only need to test that once after the loop has terminated, not on every iteration; it will only leave the loop once, so you only need to check why it left the loop once. while (in >> data) works fine for all blank streams.

    Feb 25, 2013 at 14:09

  • 3

    What you are saying (and a point made earlier) is that a bad formatted stream can be identified as !eof & fail past loop. There are cases in which one can not rely on this. See above comment (goo.gl/9mXYX). Eitherway, I am not proposing eof-check as the-always-better alternative. I am merely saying, it is a possible and (in some cases more appropriate) way of doing this, rather than “most certainly wrong!” as it tends to be claimed around here in SO.

    – sly

    Feb 25, 2013 at 15:58


  • 2

    “As an example, consider how you’d check for errors where the data is a struct with overloaded operator>> reading multiple fields at once” – a much simpler case supporting your point is stream >> my_int where the stream contains e.g. “-“: eofbit and failbit are set. That’s worse than the operator>> scenario, where the user-provided overload at least has the option of clearing eofbit before returning to help support while (s >> x) usage. More generally, this answer could use a clean-up – only the final while( !(in>>ws).eof() ) is generally robust, and it’s buried at the end.

    Feb 25, 2015 at 6:09


  • With your “3 examples” only the first sets eof. The third does not because the a fails conversion to int and is not read — it will be left on the input.

    Apr 17 at 20:54


  • so? that’s is kind of the point: early break with a bad stream, or end-loop successfully with eof.

    – sly

    Apr 26 at 1:58


78

Because if programmers don’t write while(stream >> n), they possibly write this:

while(!stream.eof())
{
    stream >> n;
    //some work on n;
}

Here the problem is, you cannot do some work on n without first checking if the stream read was successful, because if it was unsuccessful, your some work on n would produce undesired result.

The whole point is that, eofbit, badbit, or failbit are set after an attempt is made to read from the stream. So if stream >> n fails, then eofbit, badbit, or failbit is set immediately, so its more idiomatic if you write while (stream >> n), because the returned object stream converts to false if there was some failure in reading from the stream and consequently the loop stops. And it converts to true if the read was successful and the loop continues.

1

  • 2

    Apart from the mentioned “undesired result” with doing work on the undefined value of n, the program might also fall into an infinite loop, if the failing stream operation doesn’t consume any input.

    – mastov

    Apr 27, 2018 at 13:33