C++ File I/O Checking end of file inside a loop condition, bad practice?


Example

eof returns true only after reading the end of file. It does NOT indicate that the next read will be the end of stream.

while (!f.eof())
{
  // Everything is OK

  f >> buffer;

  // What if *only* now the eof / fail bit is set?

  /* Use `buffer` */
}

You could correctly write:

while (!f.eof()) 
{  
  f >> buffer >> std::ws;

  if (f.fail())
    break;

  /* Use `buffer` */
}

but

while (f >> buffer)
{
  /* Use `buffer` */
}

is simpler and less error prone.

Further references:

  • std::ws: discards leading whitespace from an input stream
  • std::basic_ios::fail: returns true if an error has occurred on the associated stream