Re: [PATCH] fread.3: Explain that file position is moved after calling fread()/fwrite()

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Thu, Jun 18, 2020 at 08:37:02AM +0000, Walter Harms wrote:
> Isn't it already covered by this fragment:
>
> "If an error occurs, or the end of the file is reached, the return
> value is a short item count (or zero)."
>
> IMHO no,
> what does a "short item count" mean ?

"items of data" refers to nmemb:

"The function fread() reads nmemb items of data"

so "item count" means number of size-long sequences of bytes and
"short" means fewer than requested. But I think you know that well and
you just think that "short item count" is not clear enough, right?

(And of course I hope that I get it right.)

> E.g. my size is 4 bytes and there are 3 bytes left to read. what is
> the return value?

If the requested size is 4 but there are only 3 bytes left then
fread() will return 0, no matter what's the value of nmemb because
it's not able to read even a single item and feof() will return
non-zero. I think that this is explained clearly enough in the
manpage.

> and will the last few bytes ever read ?

That is interesting. POSIX
https://pubs.opengroup.org/onlinepubs/9699919799.2008edition/ says
that "If a partial element is read, its value is unspecified.". But as
fread() returns number of items it has successfully read the returned
value should always be checked and it's different than requested nmemb
it means an error and I guess there is no way to tell how many bytes
have been partially read - for example if size == 4 and nmemb == 1 and
returned value is 0 you don't know if fread() read 1, 2 or 3 bytes.

Anyway, the patch that I sent explains that file pointer is moved
after fread()/fwrite(). The discussion about return value should be
started in a separate thread.

-- 
Arkadiusz Drabczyk <arkadiusz@xxxxxxxxxxxx>



[Index of Archives]     [Kernel Documentation]     [Netdev]     [Linux Ethernet Bridging]     [Linux Wireless]     [Kernel Newbies]     [Security]     [Linux for Hams]     [Netfilter]     [Bugtraq]     [Yosemite News]     [MIPS Linux]     [ARM Linux]     [Linux RAID]     [Linux Admin]     [Samba]

  Powered by Linux