Article View: comp.std.c
Article #1795Is it an error to read past end of file?
From: Paul Eggert
Date: Sat, 06 Oct 1990 00:09
Date: Sat, 06 Oct 1990 00:09
24 lines
1423 bytes
1423 bytes
Is it an error to read past EOF? For example, suppose there is just one '\n'
character left in in the stream F and we execute `C=getc(F);' three times, so
that C gets the values '\n', EOF, and EOF, respectively. What is ferror(F)
afterwards, assuming that it started off being zero?
C = getc(F); assert(C=='\n' && !feof(F) && !ferror(F));
C = getc(F); assert(C==EOF && feof(F) && !ferror(F));
C = getc(F); assert(C==EOF && feof(F) && ? );
Here is less academic example:
char b[BUFSIZ];
size_t n;
while ((n = fread(b, sizeof(char), BUFSIZ, stdin)) != 0)
if (fwrite(b, sizeof(char), n, stdout) != n)
write_error();
if (ferror(stdin))
read_error();
Suppose BUFSIZ is 1024, stdin contains only 500 bytes, and the first call to
fread() yields n=500 and sets feof(stdin). The second call to fread()
attempts to read past end of file, and yields n=0; does it also set
ferror(stdin)?
Technical Details
Message-ID:
<1990Oct6.000928.5178@twinsun.com>Path:
arc1-ca.pugleaf.net!utzoo!attcan!uunet!jarthur!ucivax!ucla-cs!twinsun!eggertShow Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!utzoo!attcan!uunet!jarthur!ucivax!ucla-cs!twinsun!eggert From: eggert@ata.twinsun.com (Paul Eggert) Newsgroups: comp.std.c Subject: Is it an error to read past end of file? Message-ID: <1990Oct6.000928.5178@twinsun.com> Date: 6 Oct 90 00:09:28 GMT Sender: news@twinsun.com Organization: Twin Sun, Inc Lines: 24 Originator: eggert@ata Nntp-Posting-Host: ata