This is the mail archive of the
libc-alpha@sourceware.cygnus.com
mailing list for the glibc project.
Re: [Gurusamy Sarathy <gsar@activestate.com>] ferror() after fread() on a FILE* opened for write
- To: libc-alpha Mailinglist <libc-alpha@sourceware.cygnus.com>, gsar@activestate.com
- Subject: Re: [Gurusamy Sarathy <gsar@activestate.com>] ferror() after fread() on a FILE* opened for write
- From: Andreas Jaeger <aj@arthur.rhein-neckar.de>
- Date: 21 Jul 1999 17:35:44 +0200
- References: <u87lnumrqp.fsf@arthur.rhein-neckar.de> <jepv1ma114.fsf@hawking.suse.de>
>>>>> Andreas Schwab writes:
> Andreas Jaeger <aj@arthur.rhein-neckar.de> writes:
> |> Looking at the appended bug report, I guess we should check for
> |> if (fp->_flags & _IO_NO_WRITES)
> |>
> |> in the fread implementation. But what's the right place and who
> |> exactly should we change it?
> I cannot find anything in the standard that requires setting the error
> indicator when writing to a read-only stream or reading from a write-only
> stream. Currently we only check _IO_NO_{READS,WRITES} in the underflow
> and overflow methods, so that getc/putc will fail (but getc doesn't set
> the error indicator yet; I'm fixing that). Neither xsgetn nor xsputn do
Thanks for the fix. Should this go also into glibc 2.1.2?
> any explicit checking, so they will fail only if the underlying descriptor
> is truely write-only or read-only, resp. (but the standard descriptors are
> read/write).
I've modified the program to use fopen:
#include <stdio.h>
int
main(int argc, char **argv)
{
char buf[500];
FILE *f;
int r;
f = fopen ("/tmp/foo", "a");
r = fread(buf, 1, 1, f);
printf("%d ferror=%d feof=%d\n", r, ferror(f), feof(f));
return 0;
}
The output of the program is:
$ ./ferror
0 ferror=0 feof=0
The ISO C9x draft I've got here, mentions as return value for fread:
[#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.
fread returned 0 which is less than 1 - therefore either a read error
or end-of-file is encountered. But feof and ferror tell me that
neither is encountered.
Is this a valid interpretation of the standard?
Andreas
--
Andreas Jaeger aj@arthur.rhein-neckar.de jaeger@informatik.uni-kl.de
for pgp-key finger ajaeger@aixd1.rhrk.uni-kl.de