opinions of error functions

Discussion in 'C Programming' started by Bill Cunningham, Feb 12, 2008.

  1. I just wanted to get some opinions on FIlE streams and error checking
    methods. I thought about EOF, NULL, and ferror. I have had trouble with EOF
    so there must be something there I'm not doing right. As far as style or a
    commonly used method is EOF perferred over ferror? Here's a snippet

    char buf[20];
    FILE *fp;
    fp=fopen("file","rb+");
    if(fread (buf,20,sizeof(char),fp)==EOF) {fclose(fp);}
    /* I know I need error checking as well as EOF but this is a skeleton that
    just opens and closes a stream */

    Would it be less messy to use ferror() or NULL? Is there a perferred way?

    Bill
    Bill Cunningham, Feb 12, 2008
    #1
    1. Advertising

  2. "Bill Cunningham" <> writes:
    > I just wanted to get some opinions on FIlE streams and error checking
    > methods. I thought about EOF, NULL, and ferror. I have had trouble with EOF
    > so there must be something there I'm not doing right. As far as style or a
    > commonly used method is EOF perferred over ferror? Here's a snippet
    >
    > char buf[20];
    > FILE *fp;
    > fp=fopen("file","rb+");
    > if(fread (buf,20,sizeof(char),fp)==EOF) {fclose(fp);}
    > /* I know I need error checking as well as EOF but this is a skeleton that
    > just opens and closes a stream */
    >
    > Would it be less messy to use ferror() or NULL? Is there a perferred way?


    Read the documentation for the fread() function. It does *not* return
    EOF to indicate an error, or for any other reason, nor does it ever
    return NULL.

    fread() returns a result of type size_t, which indicates the number of
    items actually read. If this number doesn't match the number of items
    you asked it to read, then *either* there was an error *or* it reached
    the end of the file. The result returned by fread() doesn't tell you
    which; you can then call feof() and/or ferror() to determine what
    happened.

    Unless you're absolutely 100% certain what a function does, you
    shouldn't even think about trying to use it without reading the
    documentation first.

    --
    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 12, 2008
    #2
    1. Advertising

  3. Bill Cunningham

    Eric Sosman Guest

    Bill Cunningham wrote:
    > I just wanted to get some opinions on FIlE streams and error checking
    > methods. I thought about EOF, NULL, and ferror. I have had trouble with EOF
    > so there must be something there I'm not doing right. As far as style or a
    > commonly used method is EOF perferred over ferror? Here's a snippet
    >
    > char buf[20];
    > FILE *fp;
    > fp=fopen("file","rb+");
    > if(fread (buf,20,sizeof(char),fp)==EOF) {fclose(fp);}
    > /* I know I need error checking as well as EOF but this is a skeleton that
    > just opens and closes a stream */
    >
    > Would it be less messy to use ferror() or NULL? Is there a perferred way?


    It would be "less messy" to use a method that works ...
    Consult your favorite C textbook or reference, and read
    what it says about the value returned by fread(). Pay
    close attention to everything the fread() description
    says about the value EOF.

    --
    Eric Sosman, Feb 12, 2008
    #3
  4. Bill Cunningham wrote:
    > I just wanted to get some opinions on FIlE streams and error checking
    > methods. I thought about EOF, NULL, and ferror. I have had trouble with EOF
    > so there must be something there I'm not doing right. As far as style or a
    > commonly used method is EOF perferred over ferror? Here's a snippet
    >
    > char buf[20];
    > FILE *fp;
    > fp=fopen("file","rb+");
    > if(fread (buf,20,sizeof(char),fp)==EOF) {fclose(fp);}


    The above is a very bad idea.
    fread() returns the number of items read. This number may be zero,
    but it is _never_ less than zero. But EOF is defined as a constant
    negative integral expression.
    [BTW, sizeof(char) == 1 by definition, so you have a bit of typing
    exercice there. You also have the size and count arguments
    backwards. Your code asks for 1 item of 20 bytes. If it were not
    true that sizeof(char) == 1, we could not portably know how many
    items of 20 bytes you wanted.]

    So let's consider
    #include <stdio.h>

    void /* you might change this to int and return an error code */
    testread(char *buffer, size_t item_size, size_t n_items, FILE *f)
    {
    clearerr(f); /* make sure there are no lingering indications
    of an I/O error */
    if(fread(buffer, item_size, n_items, f) < n_items)
    {
    /* for some reason we got fewer than n_items items of size
    item_size. */
    if (feof(f))
    {
    /* we tried to read past the end of file. Handle
    that condition here. */
    }
    if (ferror(f))
    {
    /* there was an error on the input stream. Handle
    that condition here. */
    }
    }


    > /* I know I need error checking as well as EOF but this is a skeleton that
    > just opens and closes a stream */


    You don't need EOF at all. In fact, you "use" is worthless.

    > Would it be less messy to use ferror() or NULL? Is there a perferred way?


    What in heaven's name would you use NULL for?
    Martin Ambuhl, Feb 12, 2008
    #4
  5. "Martin Ambuhl" <> wrote in message
    news:...
    > Bill Cunningham wrote:
    >> I just wanted to get some opinions on FIlE streams and error checking
    >> methods. I thought about EOF, NULL, and ferror. I have had trouble with
    >> EOF so there must be something there I'm not doing right. As far as style
    >> or a commonly used method is EOF perferred over ferror? Here's a snippet
    >>
    >> char buf[20];
    >> FILE *fp;
    >> fp=fopen("file","rb+");
    >> if(fread (buf,20,sizeof(char),fp)==EOF) {fclose(fp);}

    >
    > The above is a very bad idea.
    > fread() returns the number of items read. This number may be zero, but
    > it is _never_ less than zero. But EOF is defined as a constant negative
    > integral expression.
    > [BTW, sizeof(char) == 1 by definition, so you have a bit of typing
    > exercice there. You also have the size and count arguments
    > backwards. Your code asks for 1 item of 20 bytes. If it were not
    > true that sizeof(char) == 1, we could not portably know how many
    > items of 20 bytes you wanted.]
    >
    > So let's consider
    > #include <stdio.h>
    >
    > void /* you might change this to int and return an error code */
    > testread(char *buffer, size_t item_size, size_t n_items, FILE *f)
    > {
    > clearerr(f); /* make sure there are no lingering indications
    > of an I/O error */
    > if(fread(buffer, item_size, n_items, f) < n_items)
    > {
    > /* for some reason we got fewer than n_items items of size
    > item_size. */
    > if (feof(f))
    > {
    > /* we tried to read past the end of file. Handle
    > that condition here. */
    > }
    > if (ferror(f))
    > {
    > /* there was an error on the input stream. Handle
    > that condition here. */
    > }
    > }
    >
    >
    >> /* I know I need error checking as well as EOF but this is a skeleton
    >> that just opens and closes a stream */

    >
    > You don't need EOF at all. In fact, you "use" is worthless.
    >
    >> Would it be less messy to use ferror() or NULL? Is there a perferred way?

    >
    > What in heaven's name would you use NULL for?


    I thought for error checking. THanks much Martin you answered my
    question exactly as I put it. ferror() for error checking and feof() instead
    of EOF. Above when you used.

    if(fread(buffer, item_size, n_items, f) < n_items)

    Do you need this at all? I know you are testing fread's return value so I
    understand what you are trying to do. But what about this?

    if(fread(buffer,item_size,n_items,f)==feof(f))
    { handle in these braces which I would probably just use
    fwrite(stderr,"eof error"); exit(1) which would probably throw me into
    needing the stdlib.h header.

    Bill
    Bill Cunningham, Feb 13, 2008
    #5
  6. "Bill Cunningham" <> writes:
    [...]
    > I thought for error checking. THanks much Martin you answered my
    > question exactly as I put it. ferror() for error checking and feof() instead
    > of EOF. Above when you used.
    >
    > if(fread(buffer, item_size, n_items, f) < n_items)
    >
    > Do you need this at all? I know you are testing fread's return value so I
    > understand what you are trying to do.


    fread() returns a result for a reason. Use it. If it tells you it
    didn't read all the items you asked for, *then* use feof() and/or
    ferror() to tell you why.

    > But what about this?
    >
    > if(fread(buffer,item_size,n_items,f)==feof(f))
    > { handle in these braces which I would probably just use
    > fwrite(stderr,"eof error"); exit(1) which would probably throw me into
    > needing the stdlib.h header.


    You're comparing the value returned by fread() to the value returned
    by feof().

    fread() returns the number of elements successfully read, as a size_t.

    feof() returns an indication of whether the end-of-file indicator has
    been set, either zero or non-zero.

    Why would you want to compare them to each other?
    (Answer: You wouldn't.)

    --
    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 13, 2008
    #6

  7. > You're comparing the value returned by fread() to the value returned
    > by feof().
    >
    > fread() returns the number of elements successfully read, as a size_t.
    >
    > feof() returns an indication of whether the end-of-file indicator has
    > been set, either zero or non-zero.
    >
    > Why would you want to compare them to each other?
    > (Answer: You wouldn't.)
    >

    Wow what I have learned tonight. This is answering some previous errors
    I've been getting from the standard library. I have tried to conpare return
    values of different functions just as you have said Keith and had problems.
    One I get alot is concerning main not returning an int or something in my
    code. I compiled with all warnings on but the code compiled. I would think
    good coding even with gcc -Wall you wouldn't get warnings. Maybe I'm wrong.

    Bill
    Bill Cunningham, Feb 13, 2008
    #7
  8. Bill Cunningham

    Bill Reid Guest

    Keith Thompson <> wrote in message
    news:...
    > "Bill Cunningham" <> writes:


    > > I just wanted to get some opinions on FIlE streams and error

    checking
    > > methods. I thought about EOF, NULL, and ferror. I have had trouble with

    EOF
    > > so there must be something there I'm not doing right. As far as style or

    a
    > > commonly used method is EOF perferred over ferror? Here's a snippet
    > >
    > > char buf[20];
    > > FILE *fp;
    > > fp=fopen("file","rb+");
    > > if(fread (buf,20,sizeof(char),fp)==EOF) {fclose(fp);}
    > > /* I know I need error checking as well as EOF but this is a skeleton

    that
    > > just opens and closes a stream */
    > >
    > > Would it be less messy to use ferror() or NULL? Is there a perferred

    way?
    >
    > Read the documentation for the fread() function. It does *not* return
    > EOF to indicate an error, or for any other reason, nor does it ever
    > return NULL.
    >
    > Unless you're absolutely 100% certain what a function does, you
    > shouldn't even think about trying to use it without reading the
    > documentation first.


    And considering the quality of most documentation, you shouldn't
    even think about trying to use any foreign library functions at all...

    ---
    William Ernest Reid
    Bill Reid, Feb 13, 2008
    #8
  9. Keith Thompson <> wrote:
    > "Bill Cunningham" <> writes:
    > > if(fread(buffer,item_size,n_items,f)==feof(f))

    ....
    > You're comparing the value returned by fread() to the
    > value returned by feof().
    >
    > fread() returns the number of elements successfully
    > read, as a size_t.
    >
    > feof() returns an indication of whether the end-of-file
    > indicator has been set, either zero or non-zero.
    >
    > Why would you want to compare them to each other?
    > (Answer: You wouldn't.)


    Even if you did, chances are you'd quickly find out why
    it's a stupid thing to do when the first successful read
    gets reported as an error! ;-)

    --
    Peter
    Peter Nilsson, Feb 13, 2008
    #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. Kevin Spencer

    Re: another request for OPINIONS

    Kevin Spencer, Jul 7, 2003, in forum: ASP .Net
    Replies:
    5
    Views:
    335
    msnews.microsoft.com
    Jul 14, 2003
  2. Utter Newbie
    Replies:
    0
    Views:
    455
    Utter Newbie
    Jul 28, 2003
  3. JPerrin

    Opinions of Hostdepartment

    JPerrin, Oct 28, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    334
    JPerrin
    Oct 28, 2003
  4. JoeBlack
    Replies:
    2
    Views:
    351
    JoeBlack
    Oct 4, 2004
  5. Xiangliang Meng
    Replies:
    1
    Views:
    1,576
    Victor Bazarov
    Jun 21, 2004
Loading...

Share This Page