Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is "short item count" in fread()?

Tags:

fread

When I was 'man fread', I got this:

RETURN VALUE
fread() and fwrite() return the number of items successfully read or written (i.e., not the number of characters). If an error occurs, or the end- of-file is reached, the return value is a short item count (or zero).

fread() does not distinguish between end-of-file and error, and callers must use feof(3) and ferror(3) to determine which occurred.

So my question is to how to understand "short item count". Please bear with my English. Why here involves type "short"? Can you someone give an example of what does "short item count" look like? Thanks.

like image 614
zhanxw Avatar asked Jun 14 '11 03:06

zhanxw


2 Answers

The meaning of "short" in fread man page does not refer to a data type.

"Short" in this case means "less then expected". If fread() expected to read 4 objects, but only read 3, it will return the value 3.

I believe that the the man page should be re-written to say: "If an error occurs, or the end-of-file is reached, the return value is the number of items successfully read or written up until the error or EOF occurred.

like image 163
jmd Avatar answered Sep 18 '22 04:09

jmd


If you want 4, and you have 3, then you're short 1.

like image 39
Ignacio Vazquez-Abrams Avatar answered Sep 22 '22 04:09

Ignacio Vazquez-Abrams