Question on while(!feof(fp))

Discussion in 'C Programming' started by Willem, Jan 30, 2008.

  1. Willem

    Willem Guest

    As we all know, the following code is not quite correct:

    while (!feof(fp)) { fgets(...); /* do_something */ }

    And should be replaced by:

    while (fgets(...)) { /* do_something */ }


    But can the faulty code have the result that the loop body is *not*
    called, even though there is data in the file ?

    I'd say no, because there hasn't been anything yet that could have set the
    end-of-file indicator, but I have a program which copies data and once in
    a while, it produices an empty file.


    SaSW, Willem
    --
    Disclaimer: I am in no way responsible for any of the statements
    made in the above text. For all I know I might be
    drugged or something..
    No I'm not paranoid. You all think I'm paranoid, don't you !
    #EOT
     
    Willem, Jan 30, 2008
    #1
    1. Advertising

  2. On Jan 30, 1:27 pm, Willem <> wrote:
    > As we all know, the following code is not quite correct:
    >
    > while (!feof(fp)) { fgets(...); /* do_something */ }
    >
    > And should be replaced by:
    >
    > while (fgets(...)) { /* do_something */ }
    >
    > But can the faulty code have the result that the loop body is *not*
    > called, even though there is data in the file ?
    >
    > I'd say no, because there hasn't been anything yet that could have set the
    > end-of-file indicator, but I have a program which copies data and once in
    > a while, it produices an empty file.


    The eof indicator for a stream is always cleared after successfully
    opening a file so if no other file operations took place on the stream
    before the call to feof() it couldn't return true. In the case of an
    empty file, the the eof indicator wouldn't get set until a read was
    attempted.

    --
    Robert Gamble
     
    Robert Gamble, Jan 30, 2008
    #2
    1. Advertising

  3. Willem

    CBFalconer Guest

    Willem wrote:
    >
    > As we all know, the following code is not quite correct:
    >
    > while (!feof(fp)) { fgets(...); /* do_something */ }
    >
    > And should be replaced by:
    >
    > while (fgets(...)) { /* do_something */ }


    Try "while (!fgets(...)) {/* do_something */}". It'll work better.

    --
    [mail]: Chuck F (cbfalconer at maineline dot net)
    [page]: <http://cbfalconer.home.att.net>
    Try the download section.


    --
    Posted via a free Usenet account from http://www.teranews.com
     
    CBFalconer, Jan 31, 2008
    #3
  4. CBFalconer <> writes:
    > Willem wrote:
    >> As we all know, the following code is not quite correct:
    >>
    >> while (!feof(fp)) { fgets(...); /* do_something */ }
    >>
    >> And should be replaced by:
    >>
    >> while (fgets(...)) { /* do_something */ }

    >
    > Try "while (!fgets(...)) {/* do_something */}". It'll work better.


    Um, how so? fgets() returns its first argument on success, or a null
    pointer if nothing is read.

    --
    Keith Thompson (The_Other_Keith) <>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
     
    Keith Thompson, Jan 31, 2008
    #4
  5. Willem

    CBFalconer Guest

    Keith Thompson wrote:
    > CBFalconer <> writes:
    >> Willem wrote:
    >>

    .... snip ...
    >>>
    >>> while (fgets(...)) { /* do_something */ }

    >>
    >> Try "while (!fgets(...)) {/* do_something */}". It'll work better.

    >
    > Um, how so? fgets() returns its first argument on success, or a
    > null pointer if nothing is read.


    Um. I assumed do_something fixed up the error. You didn't.

    --
    [mail]: Chuck F (cbfalconer at maineline dot net)
    [page]: <http://cbfalconer.home.att.net>
    Try the download section.



    --
    Posted via a free Usenet account from http://www.teranews.com
     
    CBFalconer, Feb 1, 2008
    #5
  6. CBFalconer <> writes:
    > Keith Thompson wrote:
    >> CBFalconer <> writes:
    >>> Willem wrote:
    >>>

    > ... snip ...
    >>>>
    >>>> while (fgets(...)) { /* do_something */ }
    >>>
    >>> Try "while (!fgets(...)) {/* do_something */}". It'll work better.

    >>
    >> Um, how so? fgets() returns its first argument on success, or a
    >> null pointer if nothing is read.

    >
    > Um. I assumed do_something fixed up the error. You didn't.


    An odd assumption on your part, IMHO. The original question was about
    while (!feof(fp)) { fgets(...); /* do_something */ }
    vs.
    while (fgets(...)) { /* do_something */ }
    It seemed fairly obvious to me that do_something handles the line read
    by fgets(); any error handling would happen after the loop.

    --
    Keith Thompson (The_Other_Keith) <>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
     
    Keith Thompson, Feb 1, 2008
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mantorok Redgormor

    feof usage

    Mantorok Redgormor, Sep 20, 2003, in forum: C Programming
    Replies:
    28
    Views:
    1,170
    Dave Thompson
    Sep 29, 2003
  2. Joriveek

    feof

    Joriveek, Sep 13, 2005, in forum: C Programming
    Replies:
    8
    Views:
    724
    Keith Thompson
    Sep 14, 2005
  3. question about gets() and feof().

    , Sep 16, 2005, in forum: C Programming
    Replies:
    3
    Views:
    382
    Simon Biber
    Sep 16, 2005
  4. ehui928

    A question about fscanf and feof !

    ehui928, Oct 15, 2006, in forum: C Programming
    Replies:
    7
    Views:
    3,185
    Joe Wright
    Nov 7, 2006
  5. Andy
    Replies:
    3
    Views:
    3,484
Loading...

Share This Page