Is it an error to read past end of file?
Paul Eggert
eggert at ata.twinsun.com
Sat Oct 6 10:09:28 AEST 1990
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)?
More information about the Comp.std.c
mailing list