fread / fwrite error

B

Bob Molenbroek

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
 
J

Joe Wright

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.
 
D

Don Starr

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.
 
B

Ben Pfaff


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

This usage will always return a number between 0 and 100
inclusive, because you asked fread() to read 100 items.
 
D

Don Starr

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
 
B

Ben Pfaff

Don Starr said:
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.
 
K

Kevin Easton

Don Starr said:
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.
 
G

Guest

I use:
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...
 
E

Emmanuel Delahaye

<- Chameleon -> said:
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.
</>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

fread/fwrite 2 18
fread/fwrite 34
fread/fwrite 49
H&S fread/fwrite 7
fwrite() fails when called after fread() 2
fread fwrite struct 4
fread/fwrite Portability Issues 20
Portable use of fread/fwrite with structs 2

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top