Thread: feof() from FAQ

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    84

    feof() from FAQ

    Hi guys,

    I've just read the FAQ about NOT to use feof(fp) to check if we reached the end of the file.
    It all make sense but I don't understand 1 thing...

    Code:
    while (!feof(fp))
      {
        fgets(buf, sizeof(buf), fp);
        printf ("Line %4d: %s", i, buf);
        i++;
      }
    How come that when we reach the end of the file and the fgets return NULL, buf keep its last value... Why fgets doesn't assign buf as NULL and return NULL, it seems that it only return NULL without modifying buf.... Is that how it should be??

    Many thanks !!!

  2. #2
    Registered User
    Join Date
    Nov 2006
    Location
    Coimbra, Portugal
    Posts
    64
    Quote Originally Posted by salvadoravi View Post
    Why fgets doesn't assign buf as NULL...
    Because "buf" is converted to a char* when passed to fgets(). For a function to be able to set it to NULL, a char** would be required. Try this yourself. Write a function that receives a char* as a parameter. No matter how much you change the pointer inside the function, you won't be able to affect the original pointer, outside the function.
    Last edited by Mr_Miguel; 01-25-2008 at 12:48 PM. Reason: Clarification
    Name: Miguel Martins
    Date of birth: 14th August 1987

    "He who hesitates is lost."

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I've just read the FAQ about NOT to use feof(fp) to check if we reached the end of the file.
    I believe the FAQ does not quite say that. Rather, it advises not to use feof() to control a loop that reads input. You can use it to check if the end of file has been reached, but only after a read that might have set the end of file indicator has actually been performed.
    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

  4. #4
    Registered User
    Join Date
    Dec 2007
    Posts
    84
    Thanks for your replies guys,

    I will rephrase the question.
    Why buf[0] not set to NULL when fgets read the end of the file?

    Thanks again

  5. #5
    Registered User
    Join Date
    Nov 2006
    Location
    Coimbra, Portugal
    Posts
    64
    Perhaps you mean "buf[0] set to 0"; talking about NULL doesn't make much sense there, since "buf[0]" is of type "char".

    So your idea is that fgets() should set "buf" to an empty string, am I right? Well, so far, I haven't seen any fgets() implementation that does that. All the implementations I know leave "buf" unaltered when EOF is reached.
    Name: Miguel Martins
    Date of birth: 14th August 1987

    "He who hesitates is lost."

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Why buf[0] not set to NULL when fgets read the end of the file?
    Please don't confuse NULL and '\0'. But to answer your question, that's how fgets is defined:
    The fgets function returns s if successful. If end-of- file is encountered and no characters have been read into the array, the contents of the array remain unchanged and a null pointer is returned. If a read error occurs during the operation, the array contents are indeterminate and a null pointer is returned.
    A more interesting question is why do you think fgets should set buf[0] to '\0' when the return value of fgets clearly says that the call failed in some way?
    My best code is written with the delete key.

  7. #7
    Registered User
    Join Date
    Dec 2007
    Posts
    84
    Got IT now!!!
    Thank you pros

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Wiki FAQ
    By dwks in forum A Brief History of Cprogramming.com
    Replies: 192
    Last Post: 04-29-2008, 01:17 PM
  2. Help with FAQ
    By JoshG in forum Game Programming
    Replies: 19
    Last Post: 10-29-2002, 07:31 PM
  3. FAQ Check/Lock
    By RoD in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 10-15-2002, 11:21 AM