Thread: faqs question

  1. #1
    life is a nightmare
    Join Date
    Apr 2007
    Posts
    127

    faqs question

    hi all
    i am about to finish allot of thing that was getting in my way , any way
    i am reading the FAQ now and i while i am reading i get confused about feof faq
    The function tests the end-of-file indicator, not the stream itself
    does this mean it reads from the stream (FILE *) then it checks the buffer
    i mean like that
    -------file-------
    a
    a
    a
    --------file--------
    Code:
    while(!feof(file))
        {
            fgets(buffer,sizeof(buffer),fp);
            printf("%s\n",buffer);
         }
    it will read and print "a a a " then it will read the EOF it self and print it or what ?
    sorry it might be a really dump question but i couldn't get it
    thanks

  2. #2
    Registered User
    Join Date
    Jun 2007
    Posts
    99
    feof() returns 0 if EOF is not reached

    so !feof() , return 1 and loop will run until EOF is reached

    EOF will not get printed

  3. #3
    life is a nightmare
    Join Date
    Apr 2007
    Posts
    127
    Quote Originally Posted by vb.bajpai View Post
    feof() returns 0 if EOF is not reached

    so !feof() , return 1 and loop will run until EOF is reached

    EOF will not get printed
    sorry but you didn't add any thing new
    i am asking why i has printed " garbage " and didn't return any thing but 0 when the file EOF has reached does it read the test ?

  4. #4
    life is a nightmare
    Join Date
    Apr 2007
    Posts
    127
    and there is another things
    first
    char buf[BUFSIZ] = "Garbage";
    is BUFSIZ is a macro defined in stdio ?

    actually i am wondering how did it work in FAQ's source with out #define it ?

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    feof() is a state (in response to past events), not a prediction (of future events).

    Compare your code with
    Code:
    while(!feof(file))
        {
            if ( fgets(buffer,sizeof(buffer),fp) != NULL )
            {
                printf("%s\n",buffer);
            }
         }
    Or perhaps
    Code:
    while ( fgets(buffer,sizeof(buffer),fp) != NULL )
    {
        printf("%s\n",buffer);
    }
    // here, you use feof() or ferror() to find out the cause of the problem
    You should initially check the success/fail of a file reading function. If the read fails, then you use feof() to discover more.

    Yes, BUFSIZ is a macro defined in stdio.h
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    life is a nightmare
    Join Date
    Apr 2007
    Posts
    127
    feof() is a state (in response to past events), not a prediction (of future events).
    so that's why
    and the call to feof() returns FALSE, and we start to go through the loop again
    right ?

  7. #7
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    Quote Originally Posted by St0rM-MaN View Post
    so that's why

    right ?
    Yes, which evetually ending up with printing the grabage value.

    Quote Originally Posted by Salem
    Code:
    while(!feof(file))
    Shouldn't this be
    Code:
    while(!feof(fp))
    ssharish2005
    Last edited by ssharish2005; 06-17-2007 at 11:28 AM.

  8. #8
    life is a nightmare
    Join Date
    Apr 2007
    Posts
    127
    Quote Originally Posted by ssharish2005 View Post
    Yes, which evetually ending up with printing the grabage value.



    Shouldn't this be
    Code:
    while(!feof(fp))
    ssharish2005
    thanks and yeah

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Alice....
    By Lurker in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 06-20-2005, 02:51 PM
  2. Debugging question
    By o_0 in forum C Programming
    Replies: 9
    Last Post: 10-10-2004, 05:51 PM
  3. Question about pointers #2
    By maxhavoc in forum C++ Programming
    Replies: 28
    Last Post: 06-21-2004, 12:52 PM
  4. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  5. Question, question!
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-24-2001, 01:47 AM