Page 1 of 1

Error in Open Group Base Spec 6 for fread()

Posted: Mon Jan 13, 2014 11:22 pm
by dmatlack
So I'm reading http://pubs.opengroup.org/onlinepubs/00 ... fread.html and the example code just doesn't look right...

Code: Select all

#include <stdio.h>
...
size_t bytes_read;
char buf[100];
FILE *fp;
...
bytes_read = fread(buf, sizeof(buf), 1, fp);
...
fread(ptr, size, nitems, stream) only returns the number of bytes read if size is 1, but in the example code size is sizeof(buf) and nitems is 1. Thus this call to fread() will either return 1 on success, 0 if EOF reached, or < 0 if error.

Am I crazy?

Re: Error in Open Group Base Spec 6 for fread()

Posted: Mon Jan 13, 2014 11:52 pm
by Brendan
Hi,
dmatlack wrote:fread(ptr, size, nitems, stream) only returns the number of bytes read if size is 1, but in the example code size is sizeof(buf) and nitems is 1. Thus this call to fread() will either return 1 on success, 0 if EOF reached, or < 0 if error.

Am I crazy?
Does it matter if you ask for one item (that is "sizeof(buf)" bytes big) or ask for "sizeof(buf)" items that are 1 byte big? The only real difference is the return value.

The only problem I see here is that the prototype for "fread()" is (in my opinion) a bit stupid - it should have been "size_t fread(void *restrict ptr, size_t nbytes, FILE *restrict stream);" and return the number of bytes read. Sadly it's hard to fix standards once they're adopted, so we're stuck with it forever.


Cheers,

Brendan

Re: Error in Open Group Base Spec 6 for fread()

Posted: Mon Jan 13, 2014 11:53 pm
by Brynet-Inc
The latest is Issue 7, 2013 or POSIX.1-2008 TC1-2013.

http://pubs.opengroup.org/onlinepubs/9699919799/

You can be involved in the standards process by reporting bugs to the Austin Group, they have a tracker online.

http://austingroupbugs.net/

Re: Error in Open Group Base Spec 6 for fread()

Posted: Tue Jan 14, 2014 12:06 am
by dmatlack
Brynet-Inc wrote:The latest is Issue 7, 2013 or POSIX.1-2008 TC1-2013.
Ah it's fixed there. Thanks!