From owner-cvs-usrbin Sun Nov 17 04:43:24 1996 Return-Path: owner-cvs-usrbin Received: (from root@localhost) by freefall.freebsd.org (8.7.5/8.7.3) id EAA17510 for cvs-usrbin-outgoing; Sun, 17 Nov 1996 04:43:24 -0800 (PST) Received: from mail.cs.tu-berlin.de (mail.cs.tu-berlin.de [130.149.17.13]) by freefall.freebsd.org (8.7.5/8.7.3) with ESMTP id EAA17469; Sun, 17 Nov 1996 04:42:43 -0800 (PST) Received: from campa.panke.de (anonymous214.ppp.cs.tu-berlin.de [130.149.17.214]) by mail.cs.tu-berlin.de (8.6.13/8.6.12) with ESMTP id NAA23591; Sun, 17 Nov 1996 13:32:32 +0100 Received: (from wosch@localhost) by campa.panke.de (8.6.12/8.6.12) id NAA00423; Sun, 17 Nov 1996 13:29:19 +0100 Date: Sun, 17 Nov 1996 13:29:19 +0100 From: Wolfram Schneider Message-Id: <199611171229.NAA00423@campa.panke.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 In-Reply-To: <199611170748.IAA11061@uriah.heep.sax.de> References: <199611170350.EAA02826@campa.panke.de> <199611170748.IAA11061@uriah.heep.sax.de> MIME-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 8bit Sender: owner-cvs-usrbin@FreeBSD.ORG X-Loop: FreeBSD.org Precedence: bulk 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.