Date: Sun, 17 Nov 1996 13:29:19 +0100 From: Wolfram Schneider <wosch@cs.tu-berlin.de> To: joerg_wunsch@uriah.heep.sax.de (Joerg Wunsch) Cc: bde@zeta.org.au, cvs-all@freefall.freebsd.org, CVS-committers@freefall.freebsd.org, cvs-usrbin@freefall.freebsd.org, joerg@freefall.freebsd.org Subject: Re: cvs commit: src/usr.bin/sed main.c Message-ID: <199611171229.NAA00423@campa.panke.de> In-Reply-To: <199611170748.IAA11061@uriah.heep.sax.de> References: <199611170350.EAA02826@campa.panke.de> <199611170748.IAA11061@uriah.heep.sax.de>
next in thread | previous in thread | raw e-mail | index | archive | help
J. Wunsch writes:
>Nope, my question was which standard claims that feof() could return
>wrong results after a successful fopen() but before reaching the EOF
>of the stream.
comp.lang.c Answers to Frequently Asked Questions
Section 12. Stdio
12.1: What's wrong with this code?
char c;
while((c = getchar()) != EOF) ...
A: For one thing, the variable to hold getchar's return value
must be an int. getchar() can return all possible character
values, as well as EOF. By passing getchar's return value
through a char, either a normal character might be
misinterpreted as EOF, or the EOF might be altered (particularly
if type char is unsigned) and so never seen.
References: K&R1 Sec. 1.5 p. 14; K&R2 Sec. 1.5.1 p. 16; ANSI
Sec. 3.1.2.5, Sec. 4.9.1, Sec. 4.9.7.5; ISO Sec. 6.1.2.5,
Sec. 7.9.1, Sec. 7.9.7.5; H&S Sec. 5.1.3 p. 116, Sec. 15.1,
Sec. 15.6; CT&P Sec. 5.1 p. 70; PCS Sec. 11 p. 157.
12.2: Why does the code
while(!feof(infp)) {
fgets(buf, MAXLINE, infp);
fputs(buf, outfp);
}
copy the last line twice?
A: In C, EOF is only indicated *after* an input routine has tried
to read, and has reached end-of-file. (In other words, C's I/O
is not like Pascal's.) Usually, you should just check the
return value of the input routine (fgets() in this case); often,
you don't need to use feof() at all.
References: K&R2 Sec. 7.6 p. 164; ANSI Sec. 4.9.3, Sec. 4.9.7.1,
Sec. 4.9.10.2; ISO Sec. 7.9.3, Sec. 7.9.7.1, Sec. 7.9.10.2; H&S
Sec. 15.14 p. 382.
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?199611171229.NAA00423>
