Is this a correct way to use ferror() ?

Discussion in 'C Programming' started by Spiros Bousbouras, Nov 19, 2007.

  1. /* ... */
    f = fopen("some-file" , "r") ;
    if ( f == NULL ) {
    /* Exits */
    }
    clearerr(f) ;
    while ( ( a=getc(f) ) != EOF ) {
    /* Do stuff */
    }
    if ( ferror(f) ) {
    /* Exits */
    }

    Is the call to clearerr() necessary ? If ferror() returns 0
    can one be reasonably certain that the whole file has been
    read ?
     
    Spiros Bousbouras, Nov 19, 2007
    #1
    1. Advertising

  2. Spiros Bousbouras

    santosh Guest

    In article
    <>,
    Spiros Bousbouras <> wrote on Monday 19 Nov 2007 8:52
    pm:

    > /* ... */
    > f = fopen("some-file" , "r") ;
    > if ( f == NULL ) {
    > /* Exits */
    > }
    > clearerr(f) ;
    > while ( ( a=getc(f) ) != EOF ) {
    > /* Do stuff */
    > }
    > if ( ferror(f) ) {
    > /* Exits */
    > }
    >
    > Is the call to clearerr() necessary ?


    I don't believe so.

    > If ferror() returns 0 can one be reasonably certain that the whole
    > file has been read ?


    Within the constraints of Standard C, I think that might be a reasonable
    conclusion.

    I generally test explicitly with feof() as well, even if ferror()
    returns false. Just a habit.
     
    santosh, Nov 19, 2007
    #2
    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. joon
    Replies:
    1
    Views:
    533
    Roedy Green
    Jul 8, 2003
  2. Dan

    correct or not correct?

    Dan, Oct 2, 2003, in forum: HTML
    Replies:
    7
    Views:
    469
  3. Stephen Howe

    ferror()

    Stephen Howe, Dec 10, 2003, in forum: C Programming
    Replies:
    36
    Views:
    1,525
    Stephen Howe
    Dec 30, 2003
  4. rCs

    EOF vs. feof() and ferror()

    rCs, Oct 31, 2006, in forum: C Programming
    Replies:
    8
    Views:
    1,803
    Barry Schwarz
    Nov 1, 2006
  5. , India

    question on ferror() function

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

Share This Page