EOF vs. feof() and ferror()

Discussion in 'C Programming' started by rCs, Oct 31, 2006.

  1. rCs

    rCs Guest

    Which of the following two approaches is preferred (and why)?

    int c;

    do {
    ...
    c = getchar();
    } while (c != EOF);

    - or -

    int c;

    do {
    ...
    c = getchar();
    } while (!feof(stdin) && !ferror(stdin));

    rCs
    rCs, Oct 31, 2006
    #1
    1. Advertising

  2. rCs

    Guest

    Re: EOF vs. feof() and ferror()

    rCs wrote:

    > Which of the following two approaches is preferred (and why)?
    >
    > int c;
    >
    > do {
    > ...
    > c = getchar();
    > } while (c != EOF);
    >
    > - or -
    >
    > int c;
    >
    > do {
    > ...
    > c = getchar();
    > } while (!feof(stdin) && !ferror(stdin));



    Neither - it's rarely appropriate to use a do ... while() loop in this
    context. I can't imagine what processing your loop body would do before
    reading the first character.

    feof() is (IMHO) rarely needed, as it's normally possible to recognise
    EOF directly.

    int c;
    while((c = getchar()) != EOF) {
    ....
    }

    would probably be my approach in all but the most obscure cases.
    , Oct 31, 2006
    #2
    1. Advertising

  3. Re: EOF vs. feof() and ferror()

    On 31 Oct 2006 05:36:41 -0800, mark_bluemel@... wrote:
    >feof() is (IMHO) rarely needed, as it's normally possible to recognise
    >EOF directly.
    >
    >int c;
    >while((c = getchar()) != EOF) {
    >...
    >}
    >
    >would probably be my approach in all but the most obscure cases.


    Ok, but after the loop you need to distinguish between EOF and error,
    e.g. with feof() or ferror().

    Best wishes,
    Roland Pibinger
    Roland Pibinger, Oct 31, 2006
    #3
  4. Re: EOF vs. feof() and ferror()

    On 31 Oct 2006 05:36:41 -0800, in comp.lang.c ,
    wrote:

    >feof() is (IMHO) rarely needed, as it's normally possible to recognise
    >EOF directly.


    Depends on the context. There's a FAQ about the use of feof().

    --
    Mark McIntyre

    "Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are,
    by definition, not smart enough to debug it."
    --Brian Kernighan
    Mark McIntyre, Oct 31, 2006
    #4
  5. rCs

    Jordan Abel Guest

    Re: EOF vs. feof() and ferror()

    2006-10-31 <>,
    wrote:
    > feof() is (IMHO) rarely needed, as it's normally possible to recognise
    > EOF directly.


    the point of feof() is to find out _why_ you got EOF.
    Jordan Abel, Oct 31, 2006
    #5
  6. rCs

    John Bode Guest

    Re: EOF vs. feof() and ferror()

    rCs wrote:
    > Which of the following two approaches is preferred (and why)?
    >
    > int c;
    >
    > do {
    > ...
    > c = getchar();
    > } while (c != EOF);
    >
    > - or -
    >
    > int c;
    >
    > do {
    > ...
    > c = getchar();
    > } while (!feof(stdin) && !ferror(stdin));
    >
    > rCs


    Neither option's that good, but of the two the first one is preferable.
    You always want to test the value returned from getchar() for EOF,
    rather than relying on feof() to tell you when you've hit the
    end-of-file. feof() won't return true until *after* you've tried to
    read beyond the end of the file, so you wind up looping once too often.


    I'd rewrite it as

    while ((c = getchar() ) != EOF)
    {
    /* do something with c */
    }

    if (feof(stdin))
    {
    /* handle end-of-file condition */
    }
    else
    {
    /* handle error condition */
    }
    John Bode, Oct 31, 2006
    #6
  7. rCs

    Simon Biber Guest

    Re: EOF vs. feof() and ferror()

    John Bode wrote:
    > rCs wrote:
    >> Which of the following two approaches is preferred (and why)?
    >>
    >> int c;
    >>
    >> do {
    >> ...
    >> c = getchar();
    >> } while (c != EOF);
    >>
    >> - or -
    >>
    >> int c;
    >>
    >> do {
    >> ...
    >> c = getchar();
    >> } while (!feof(stdin) && !ferror(stdin));
    >>
    >> rCs

    >
    > Neither option's that good, but of the two the first one is preferable.
    > You always want to test the value returned from getchar() for EOF,
    > rather than relying on feof() to tell you when you've hit the
    > end-of-file. feof() won't return true until *after* you've tried to
    > read beyond the end of the file, so you wind up looping once too often.


    Both options given by rCs will loop the same number of times. In fact,
    their behaviour is equivalent. When getchar() has returned EOF, then one
    of feof(stdin) or ferror(stdin) must be true. The only cost is the time
    spent making two additional function calls per loop, which is likely to
    be negligible.

    You could say that both of them loop once too often. That is on the
    first time through the loop, when c is uninitialised.

    > I'd rewrite it as
    >
    > while ((c = getchar() ) != EOF)
    > {
    > /* do something with c */
    > }


    This is better merely because it avoids the first time through the loop
    when c was uninitialised.

    > if (feof(stdin))
    > {
    > /* handle end-of-file condition */
    > }
    > else
    > {
    > /* handle error condition */
    > }
    >


    rCs's code would still have to perform this test or equivalent, after
    the loop finished.

    --
    Simon.
    Simon Biber, Nov 1, 2006
    #7
  8. Re: EOF vs. feof() and ferror()

    On 31 Oct 2006 14:37:15 -0800, "John Bode" <>
    wrote:

    >
    >rCs wrote:
    >> Which of the following two approaches is preferred (and why)?
    >>
    >> int c;
    >>
    >> do {
    >> ...
    >> c = getchar();
    >> } while (c != EOF);
    >>
    >> - or -
    >>
    >> int c;
    >>
    >> do {
    >> ...
    >> c = getchar();
    >> } while (!feof(stdin) && !ferror(stdin));
    >>
    >> rCs

    >
    >Neither option's that good, but of the two the first one is preferable.
    > You always want to test the value returned from getchar() for EOF,
    >rather than relying on feof() to tell you when you've hit the
    >end-of-file. feof() won't return true until *after* you've tried to
    >read beyond the end of the file, so you wind up looping once too often.


    If c is set to EOF by getchar, then either feof or ferror must return
    true. If c is not set to EOF by getchar, then both must return false.
    Ergo, the two while clauses are logically equivalent.


    Remove del for email
    Barry Schwarz, Nov 1, 2006
    #8
  9. On 31 Oct 2006 05:27:44 -0800, "rCs" <> wrote:

    >Which of the following two approaches is preferred (and why)?
    >
    >int c;
    >
    >do {
    > ...
    > c = getchar();
    >} while (c != EOF);
    >
    > - or -
    >
    >int c;
    >
    >do {
    > ...
    > c = getchar();
    >} while (!feof(stdin) && !ferror(stdin));
    >


    The two while clauses are logically equivalent but the first avoids
    two function calls (only one when EOF is finally detected) and
    possibly an extra comparison (until EOF is detected).


    Remove del for email
    Barry Schwarz, Nov 1, 2006
    #9
    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. Stephen Howe

    ferror()

    Stephen Howe, Dec 10, 2003, in forum: C Programming
    Replies:
    36
    Views:
    1,475
    Stephen Howe
    Dec 30, 2003
  2. Kobu
    Replies:
    10
    Views:
    608
    Keith Thompson
    Mar 4, 2005
  3. Spiros Bousbouras

    Is this a correct way to use ferror() ?

    Spiros Bousbouras, Nov 19, 2007, in forum: C Programming
    Replies:
    1
    Views:
    387
    santosh
    Nov 19, 2007
  4. 2005
    Replies:
    6
    Views:
    604
    David Thompson
    Apr 21, 2008
  5. , India

    question on ferror() function

    , India, Aug 11, 2008, in forum: C Programming
    Replies:
    4
    Views:
    692
    Ian Collins
    Aug 12, 2008
Loading...

Share This Page