Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > fread / fwrite error

Reply
Thread Tools

fread / fwrite error

 
 
Guest
Posts: n/a
 
      08-01-2003
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?


 
Reply With Quote
 
 
 
 
Bob Molenbroek
Guest
Posts: n/a
 
      08-01-2003

"<- 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


 
Reply With Quote
 
 
 
 
Joe Wright
Guest
Posts: n/a
 
      08-01-2003
<- 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 private.php?do=newpm&u=
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
 
Reply With Quote
 
Don Starr
Guest
Posts: n/a
 
      08-01-2003
"<- 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.

 
Reply With Quote
 
Ben Pfaff
Guest
Posts: n/a
 
      08-01-2003
"<- 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
 
Reply With Quote
 
Don Starr
Guest
Posts: n/a
 
      08-01-2003
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

 
Reply With Quote
 
Ben Pfaff
Guest
Posts: n/a
 
      08-01-2003
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[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuv wxyz.\
\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[i]\
);}return 0;}
 
Reply With Quote
 
Kevin Easton
Guest
Posts: n/a
 
      08-02-2003
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.

 
Reply With Quote
 
Guest
Posts: n/a
 
      08-02-2003
> 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...


 
Reply With Quote
 
Emmanuel Delahaye
Guest
Posts: n/a
 
      08-07-2003
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/
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
fwrite() fails when called after fread() Richard Hsu C Programming 2 04-11-2006 09:04 PM
fread fwrite struct janssenssimon@hotmail.com C Programming 4 03-31-2006 05:47 AM
Portable use of fread/fwrite with structs mazsx C Programming 2 11-11-2005 03:55 PM
Re: fread/fwrite Bits CBFalconer C Programming 0 01-03-2004 04:02 AM
problem using fread, fwrite, and fsetpos Brady C Programming 8 07-20-2003 11:49 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57