Thread: feof()

  1. #1
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718

    feof()

    I know that feof() is not supposed to be used as a loop condition, but what is it supposed to be used for?
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by XSquared
    I know that feof() is not supposed to be used as a loop condition, but what is it supposed to be used for?
    It can be -- just not the way it is typically attempted. Here is an example from the standard.
    Code:
    #include <stdio.h>
    /* ... */
    int count; float quant; char units[21], item[21];
    do {
    count = fscanf(stdin, "%f%20s of %20s", &quant, units, item);
    fscanf(stdin,"%*[^\n]");
    } while (!feof(stdin) && !ferror(stdin));
    Another good note:
    An end-of-file and a read error can be distinguished by use of the feof and ferror functions.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User pinko_liberal's Avatar
    Join Date
    Oct 2001
    Posts
    284
    Quote Originally Posted by Dave_Sinkula
    It can be -- just not the way it is typically attempted. Here is an example from the standard.
    Code:
    #include <stdio.h>
    /* ... */
    int count; float quant; char units[21], item[21];
    do {
    count = fscanf(stdin, "%f%20s of %20s", &quant, units, item);
    fscanf(stdin,"%*[^\n]");
    } while (!feof(stdin) && !ferror(stdin));
    Another good note:
    The fgets function returns NULL in two cases : 1] If the end of file is reached and there are no more characters left to read or, 2] If an error is encountered while reading the file.

    So, when we break out of the fgets loop we cannot be certain if we could read all the file sucessfully since the return value of fgets contains no such information.

    So when can check the value of feof to check if the end of file was actually reached.
    The one who says it cannot be done should never interrupt the one who is doing it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. feof issues
    By Rare177 in forum C Programming
    Replies: 13
    Last Post: 11-23-2004, 10:11 AM
  2. feof stuff...
    By cnewbee in forum C Programming
    Replies: 1
    Last Post: 04-08-2003, 06:26 PM
  3. feof ....EOF
    By GanglyLamb in forum C Programming
    Replies: 12
    Last Post: 12-04-2002, 12:38 PM
  4. advice on feof()
    By Hobo in forum C Programming
    Replies: 2
    Last Post: 09-07-2002, 08:15 AM
  5. Is !feof supposed to work?
    By SMurf in forum C Programming
    Replies: 2
    Last Post: 08-29-2001, 12:44 PM