Error in Open Group Base Spec 6 for fread()

Programming, for all ages and all languages.
Post Reply
User avatar
dmatlack
Posts: 12
Joined: Tue Dec 17, 2013 12:32 am

Error in Open Group Base Spec 6 for fread()

Post 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?
User avatar
Brendan
Member
Member
Posts: 8561
Joined: Sat Jan 15, 2005 12:00 am
Location: At his keyboard!
Contact:

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

Post 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
For all things; perfection is, and will always remain, impossible to achieve in practice. However; by striving for perfection we create things that are as perfect as practically possible. Let the pursuit of perfection be our guide.
User avatar
Brynet-Inc
Member
Member
Posts: 2426
Joined: Tue Oct 17, 2006 9:29 pm
Libera.chat IRC: brynet
Location: Canada
Contact:

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

Post 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/
Image
Twitter: @canadianbryan. Award by smcerm, I stole it. Original was larger.
User avatar
dmatlack
Posts: 12
Joined: Tue Dec 17, 2013 12:32 am

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

Post by dmatlack »

Brynet-Inc wrote:The latest is Issue 7, 2013 or POSIX.1-2008 TC1-2013.
Ah it's fixed there. Thanks!
Post Reply