fread / fwrite error

Discussion in 'C Programming' started by Guest, Aug 1, 2003.

  1. Guest

    Guest Guest

    I use:
    ------------
    a = fread(buffer, 100, 1, file);
    ------------

    usually a = 100, but there are cases where a == 1!
    There is no file problem or eof because when change to
    ------------
    a = fread(buffer, 1, 100, file);
    ------------
    there is no problem and a is always 100!

    Why?
    Maybe a library error?
    Guest, Aug 1, 2003
    #1
    1. Advertising

  2. "<- Chameleon ->" <> wrote in message
    news:bgdp8j$him$...
    > I use:
    > ------------
    > a = fread(buffer, 100, 1, file);
    > ------------
    >
    > usually a = 100, but there are cases where a == 1!
    > There is no file problem or eof because when change to
    > ------------
    > a = fread(buffer, 1, 100, file);
    > ------------
    > there is no problem and a is always 100!
    >
    > Why?
    > Maybe a library error?
    >
    >


    In what cases does the first read return 100? that puzzles me, but then
    again I am only occasionally programming in C. The rest is as I would
    expect it (that is if no eof or error occurred as you say).

    Bob
    Bob Molenbroek, Aug 1, 2003
    #2
    1. Advertising

  3. Guest

    Joe Wright Guest

    <- Chameleon -> wrote:
    >
    > I use:
    > ------------
    > a = fread(buffer, 100, 1, file);
    > ------------
    >
    > usually a = 100, but there are cases where a == 1!
    > There is no file problem or eof because when change to
    > ------------
    > a = fread(buffer, 1, 100, file);
    > ------------
    > there is no problem and a is always 100!
    >
    > Why?
    > Maybe a library error?


    You can get very tired and a lot older spending time trying to find your
    programming errors in the standard library. Let's see the program that's
    giving you these results. A small but complete program that exhibits the
    problem.
    --
    Joe Wright mailto:
    "Everything should be made as simple as possible, but not simpler."
    --- Albert Einstein ---
    Joe Wright, Aug 1, 2003
    #3
  4. Guest

    Don Starr Guest

    "<- Chameleon ->" <> wrote in message
    news:bgdp8j$him$...
    > I use:
    > ------------
    > a = fread(buffer, 100, 1, file);
    > ------------
    >
    > usually a = 100, but there are cases where a == 1!
    > There is no file problem or eof because when change to
    > ------------
    > a = fread(buffer, 1, 100, file);
    > ------------
    > there is no problem and a is always 100!
    >
    > Why?
    > Maybe a library error?
    >
    >


    size_t fread( void *buffer, size_t size, size_t count, FILE *stream );

    fread() returns the number of full items actually read. The count of items to read is specified by
    the third parameter, while the size of each item is specified by the second parameter.

    In your first example, you've told it to read (1) 100-byte long item. If successful, it will return
    1.

    In the second example, you've told it to read (100) 1-byte long items. If successful, it will return
    100.

    (where "successful" means "no error occurs and eof is not reached"; i.e. if the function reads
    exactly what you told it to read).

    If fread() returns anything other than 1 or 100, respectively, then either end-of-file was reached
    before all requested items were read or some error occured.
    Don Starr, Aug 1, 2003
    #4
  5. Guest

    Ben Pfaff Guest

    "<- Chameleon ->" <> writes:

    > I use:
    > ------------
    > a = fread(buffer, 100, 1, file);
    > ------------
    >
    > usually a = 100, but there are cases where a == 1!


    This usage will always return either 0 or 1, because fread()
    returns the number of items read and you only asked it to read
    one item.

    > There is no file problem or eof because when change to
    > ------------
    > a = fread(buffer, 1, 100, file);
    > ------------
    > there is no problem and a is always 100!


    This usage will always return a number between 0 and 100
    inclusive, because you asked fread() to read 100 items.
    --
    "When I have to rely on inadequacy, I prefer it to be my own."
    --Richard Heathfield
    Ben Pfaff, Aug 1, 2003
    #5
  6. Guest

    Don Starr Guest

    On 01 Aug 2003 13:32:00 -0700, Ben Pfaff <> wrote:

    >It always returns the count of full items read. Here is an
    >excerpt from C99 7.19.8.1 "The fread function":
    >
    > Returns
    >
    >3 The fread function returns the number of elements
    > successfully read, which may be less than nmemb if a read
    > error or end-of-file is encountered. If size or nmemb is
    > zero, fread returns zero and the contents of the array and
    > the state of the stream remain unchanged.


    Yes, I've read that description in several places (man pages, etc.). However, while it says "may be
    less than", it does not say "will not be more than".

    Perhaps I'm being a bit pedantic here, but the quoted text above does not seem to exclude the
    possibility of fread() returning a value greater than the number of elements actually requested, in
    case of error.

    If I read the above quoted text correctly, it says (and only says):
    Return is number successfully read
    If error or EOF, return MAY BE less than requested

    Logically, we cannot assume:
    If error or EOF, return MAY NOT BE greater than requested
    Perhaps it's intended that such an assumption can be made, but the text certainly doesn't say that
    explicitly (or infer it logically). What if an error caused a read of more items than requested?

    -Don
    Don Starr, Aug 1, 2003
    #6
  7. Guest

    Ben Pfaff Guest

    Don Starr <> writes:

    > On 01 Aug 2003 13:32:00 -0700, Ben Pfaff <> wrote:
    >
    > >It always returns the count of full items read. Here is an
    > >excerpt from C99 7.19.8.1 "The fread function":
    > >
    > > Returns
    > >
    > >3 The fread function returns the number of elements
    > > successfully read, which may be less than nmemb if a read
    > > error or end-of-file is encountered. If size or nmemb is
    > > zero, fread returns zero and the contents of the array and
    > > the state of the stream remain unchanged.

    >
    > Yes, I've read that description in several places (man pages, etc.). However, while it says "may be
    > less than", it does not say "will not be more than".


    The previous paragraph says this:

    The fread function reads, into the array pointed to by ptr,
    up to nmemb elements whose size is specified by size, from
    the stream pointed to by stream.

    It reads "up to nmemb elements" and returns a value that "may be
    less than nmemb". You figure it out.
    --
    int main(void){char p[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz.\
    \n",*q="kl BIcNBFr.NKEzjwCIxNJC";int i=sizeof p/2;char *strchr();int putchar(\
    );while(*q){i+=strchr(p,*q++)-p;if(i>=(int)sizeof p)i-=sizeof p-1;putchar(p\
    );}return 0;}
    Ben Pfaff, Aug 2, 2003
    #7
  8. Guest

    Kevin Easton Guest

    Don Starr <> wrote:
    > On 01 Aug 2003 16:24:49 -0700, Ben Pfaff <> wrote:
    >
    >>Don Starr <> writes:
    >>
    >>> On 01 Aug 2003 13:32:00 -0700, Ben Pfaff <> wrote:
    >>>
    >>> >It always returns the count of full items read. Here is an
    >>> >excerpt from C99 7.19.8.1 "The fread function":
    >>> >
    >>> > Returns
    >>> >
    >>> >3 The fread function returns the number of elements
    >>> > successfully read, which may be less than nmemb if a read
    >>> > error or end-of-file is encountered. If size or nmemb is
    >>> > zero, fread returns zero and the contents of the array and
    >>> > the state of the stream remain unchanged.
    >>>
    >>> Yes, I've read that description in several places (man pages, etc.). However, while it says "may be
    >>> less than", it does not say "will not be more than".

    >>
    >>The previous paragraph says this:
    >>
    >> The fread function reads, into the array pointed to by ptr,
    >> up to nmemb elements whose size is specified by size, from
    >> the stream pointed to by stream.
    >>
    >>It reads "up to nmemb elements" and returns a value that "may be
    >>less than nmemb". You figure it out.

    >
    > I'm still trying to figure it out. I have not yet seen any information that allows me to logically
    > conclude that the return value will not be greater than the specified count.
    >
    > I've seen
    > * "reads up to" specified count
    > * returns count of items read


    Combine these two, and you get "returns up to specified count".

    - Kevin.
    Kevin Easton, Aug 2, 2003
    #8
  9. Guest

    Guest Guest

    > I use:
    > ------------
    > a = fread(buffer, 100, 1, file);
    > ------------
    >
    > usually a = 100, but there are cases where a == 1!
    > There is no file problem or eof because when change to
    > ------------
    > a = fread(buffer, 1, 100, file);
    > ------------
    > there is no problem and a is always 100!


    Sorry kids!
    My program are multithreaded and my variable are static. So, another thread
    changes my variable.
    I must use mutual exclusion.
    Thanks for your response and sorry...
    Guest, Aug 2, 2003
    #9
  10. In 'comp.lang.c', "<- Chameleon ->" <> wrote:

    > My program are multithreaded and my variable are static. So, another thread
    > changes my variable.
    > I must use mutual exclusion.


    <OT>
    Huh! What about contextual data instead? Better not to use globals in a
    multi-thread application at all.
    </>

    --
    -ed- [remove YOURBRA before answering me]
    The C-language FAQ: http://www.eskimo.com/~scs/C-faq/top.html
    <blank line>
    FAQ de f.c.l.c : http://www.isty-info.uvsq.fr/~rumeau/fclc/
    Emmanuel Delahaye, Aug 7, 2003
    #10
    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. Brady

    problem using fread, fwrite, and fsetpos

    Brady, Jul 17, 2003, in forum: C Programming
    Replies:
    8
    Views:
    933
    Dave Thompson
    Jul 21, 2003
  2. CBFalconer

    Re: fread/fwrite Bits

    CBFalconer, Jan 3, 2004, in forum: C Programming
    Replies:
    0
    Views:
    841
    CBFalconer
    Jan 3, 2004
  3. mazsx
    Replies:
    2
    Views:
    853
    Toni Uusitalo
    Nov 11, 2005
  4. fread fwrite struct

    , Mar 30, 2006, in forum: C Programming
    Replies:
    4
    Views:
    447
    Michael Mair
    Mar 31, 2006
  5. Richard Hsu

    fwrite() fails when called after fread()

    Richard Hsu, Apr 11, 2006, in forum: C Programming
    Replies:
    2
    Views:
    505
    Richard Hsu
    Apr 11, 2006
Loading...

Share This Page