Thread: EOF Statement in C++

  1. #1
    Registered User Vespasian's Avatar
    Join Date
    Aug 2011
    Posts
    181

    EOF Statement in C++

    It is commonly advised that using the EOF flag for file input is erroneous in C++. The follwoing article explains why:

    EOF And Reading Text Files (C++) - C++ Tutorials | Dream.In.Code

    I especially don't get the paragraph:

    Uh oh! First the program tells you that its read 6 names when there are only 5 names in the file, and then it goes on to display the last name twice.

    What has happened is that the while loop has been repeated 6 times, because it is repeating until namefile has its internal "EOF" flag set. Remember that ifstream is not a file, its a stream, which means that it cannot know whether or not there is any more data in a file until after a failed attempt to read past the end of the file. After it read "Harry" from the file, the stream had not failed, therefore the EOF flag was still unset.

    There's an even worse (more subtle) problem with this, based on how the >> operator works; it will always stop reading at the first 'whitespace' character it encounters - a space, a newline, a carriage return or a tab.
    In lamens terms, why, using EOF, does the last entry or character get repeated twice?

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    because eof flag is set AFTER the failed read attemp

    So 5th successful read will not set the flag even when this record is last one in the file
    Next 6th attempt will fail, set the flag to true and leave teh buffer with last read data (from 5th read)

    After loop prints that 5th result second time it will exit.

    So instead of checking the eof flag - the result of the read operation should be checked
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Registered User Vespasian's Avatar
    Join Date
    Aug 2011
    Posts
    181
    Thanks.

    What other solution is proposed apart from the one mentioned in the link.

    I.e. they propose:

    while (fileinput >> stream)

    Is this the best and only option?

  4. #4
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    you can implement any other approach that checks the state of the stream before using the read operation result
    Code:
       for(;;)
        {
           fileinput >> result;
           if(!fileinput )
           {
              // read failed
              if( fileinput.eof())
              {
                 //end of file - just exit the loop
                 break;
              }
              if( fileinput.bad())
              {
                  //Read/writing error on i/o operation
                  //log error and exit the loop
    
                  break;
              }
              if( fileinput.fail())
              {
                  //Logical error on i/o operation
                  //need to skip mailformed data and continue reading
    
                  ...
    
                  continue;
              }
              break;
           }
    
           //if we got here - reading was successful - use result
        }
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by Vespasian View Post
    Thanks.

    What other solution is proposed apart from the one mentioned in the link.

    I.e. they propose:

    while (fileinput >> stream)

    Is this the best and only option?
    It's certainly not the best. This checks if the read failed, which could be for many reasons.
    It's usually a good enough way to do it.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Just in case it's relevant, I want to mention that you should compare the return value of a function that might return end-of-file, such as istream::peek(), to char_traits<return_type>::eof().

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to use if else statement?
    By akif13 in forum C Programming
    Replies: 5
    Last Post: 12-08-2012, 01:08 PM
  2. If statement hw help
    By AlexTank853 in forum C Programming
    Replies: 10
    Last Post: 09-22-2012, 01:28 PM
  3. Statement inside a statement.
    By JOZZY& Wakko in forum C Programming
    Replies: 15
    Last Post: 11-05-2009, 03:18 PM
  4. Help with if statement
    By nafix in forum C Programming
    Replies: 5
    Last Post: 09-14-2007, 05:49 PM
  5. if statement
    By ZakkWylde969 in forum C++ Programming
    Replies: 22
    Last Post: 07-11-2003, 10:48 PM

Tags for this Thread