opinions of error functions

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Bill Cunningham

    opinions of error functions

    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


  • Eric Sosman

    #2
    Re: opinions of error functions

    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@sun .com

    Comment

    • Martin Ambuhl

      #3
      Re: opinions of error functions

      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?

      Comment

      • Bill Cunningham

        #4
        Re: opinions of error functions


        "Martin Ambuhl" <mambuhl@earthl ink.netwrote in message
        news:61emftF1qg gadU1@mid.indiv idual.net...
        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_it ems,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


        Comment

        • Keith Thompson

          #5
          Re: opinions of error functions

          "Bill Cunningham" <nospam@nspam.c omwrites:
          [...]
          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_it ems,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_Keit h) <kst-u@mib.org>
          Nokia
          "We must do something. This is something. Therefore, we must do this."
          -- Antony Jay and Jonathan Lynn, "Yes Minister"

          Comment

          • Bill Cunningham

            #6
            Re: opinions of error functions

            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


            Comment

            • Peter Nilsson

              #7
              Re: opinions of error functions

              Keith Thompson <ks...@mib.orgw rote:
              "Bill Cunningham" <nos...@nspam.c omwrites:
              if(fread(buffer ,item_size,n_it ems,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

              Comment

              Working...