Thread: Differences between EOF and feof() in FILES

  1. #1
    Banned
    Join Date
    Apr 2015
    Posts
    596

    Differences between EOF and feof() in FILES

    Hi, I've read the definitions of EOF and feof() in C language, but still not understanding very well what are the differences of using them?
    the both are expressing "reaching end of the file!

    Can anyone please explain the differences between using those both commands? if possible with examples would be more sufficient.


    thanks!

  2. #2
    Banned
    Join Date
    Apr 2015
    Posts
    596
    And why it's wrong to write
    Code:
    while(!feof( fp ))
    in c(p is a pointer to a file)?

  3. #3
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Well, EOF is a number. feof() is a function.

    Also as for why using feof() to control a loop is wrong, read: FAQ > Why it's bad to use feof() to control a loop - Cprogramming.com

  4. #4
    Banned
    Join Date
    Apr 2015
    Posts
    596
    Quote Originally Posted by whiteflags View Post
    Well, EOF is a number. feof() is a function.

    Also as for why using feof() to control a loop is wrong, read: FAQ > Why it's bad to use feof() to control a loop - Cprogramming.com
    I'm with you.

    but is there a logically difference between writing
    Code:
     char c;
    while (c !=EOF )
    and
    Code:
     while (!feof(fp)) /*fp is a pointer to a file*/
    ? the both are indicating to a condition that simplify "whenever we didn't reach the end of the file, continue with the iterations of the while loop .."

  5. #5
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    but is there a logically difference between writing:
    Yes, and you would know the difference if you bothered to read the page that I linked.

    Most of the time, when you compare against EOF, it is because you are doing it in a larger expression checking the return value of a function like fgetc(). fgetc() will return EOF if it fails. One time it can fail is if all of the file has been read, so there is no more content. Now, consider feof(), which is usually used incorrectly. It shouldn't be used to control a loop, because it depends on another function like fgetc() to fail first, and change the stream state; if that doesn't happen, then feof() will lead to unwanted results.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by RyanC
    but is there a logically difference between writing
    (...)
    ? the both are indicating to a condition that simplify "whenever we didn't reach the end of the file, continue with the iterations of the while loop .."
    Consider this:
    Code:
    int c;
    while ((c = fgetc(fp)) != EOF)
    {
        /* do something with c */
    }
    The above is fine because when end of file is reached, fgetc will return EOF, hence terminating the loop before something is done with c. Notice that c is an int because EOF is an int.

    Now, consider:
    Code:
    int c = 0;
    while (c != EOF)
    {
        c = fgetc(fp);
        /* do something with c */
    }
    The above is problematic because when end of file is reached, fgetc will return EOF, but something will be done with c, even though it is EOF, before the loop terminates, i.e., the check happens in a wrong place.

    Likewise, consider:
    Code:
    int c;
    while (!feof(fp))
    {
        c = fgetc(fp);
        /* do something with c */
    }
    The above has the exact same problem as the previous example. So, the problem is not with feof per se: the problem is with how it is used to control the loop, i.e., the apparently obvious way to use it to control a loop is wrong.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. while(!feof(f))...
    By catasturslykid in forum C Programming
    Replies: 5
    Last Post: 08-23-2013, 12:37 PM
  2. while(!feof)
    By mconflict in forum C Programming
    Replies: 12
    Last Post: 04-02-2012, 12:30 PM
  3. HELP: feof
    By dlf723 in forum C Programming
    Replies: 5
    Last Post: 07-23-2010, 08:49 AM
  4. feof() from FAQ
    By salvadoravi in forum C Programming
    Replies: 6
    Last Post: 01-25-2008, 01:08 PM
  5. feof()
    By XSquared in forum C Programming
    Replies: 2
    Last Post: 06-02-2004, 12:16 AM