This message generated a parse failure. Raw output follows here. Please use 'back' to navigate. From devnull@lkml.org Thu Apr 25 05:41:55 2024 Received: from nic.funet.fi (nic.funet.fi [128.214.248.6]) by herbie.ucs.indiana.edu (8.7.5/8.6.12) with ESMTP id NAA16474 for ; Sat, 22 Feb 1997 13:16:17 -0500 (EST) Received: from vger.rutgers.edu ([128.6.190.2]) by nic.funet.fi with ESMTP id <74049-7574>; Sat, 22 Feb 1997 20:22:20 +0200 Received: by vger.rutgers.edu id <213029-16379>; Sat, 22 Feb 1997 13:05:59 -0500 Message-Id: <330F342E.CDCBC20@pratique.fr> Date: Sat, 22 Feb 1997 19:00:14 +0100 From: Guilhem Lavaux X-Mailer: Mozilla 3.0 (X11; I; Linux 2.1.26 i586) Mime-Version: 1.0 To: Russell Berry Cc: linux-kernel@vger.rutgers.edu Subject: Re: Behavior of feof() References: Content-Type: multipart/mixed; boundary="------------1663BDF71480945943551305" Sender: owner-linux-kernel@vger.rutgers.edu Precedence: bulk This is a multi-part message in MIME format. --------------1663BDF71480945943551305 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit Russell Berry wrote: > > I've just encountered a very odd behavior of feof(); on > linux-2.0.29. In the following program: > > #include > void main(){ > > int r,g,b,result; > char cname[80]; > > FILE *rgb,*color; > > rgb=fopen("rgb.txt","r+"); > color=fopen("XColor.java","w+"); > while(!feof(rgb)){ > fscanf(rgb,"%d%d%d%s",&r,&g,&b,cname); > fprintf(color," ////////////////////\n"); > fprintf(color," // The color %s\n",cname); > fprintf(color," ////////////////////\n\n"); > fprintf(color," public final static Color %s = new\ > Color(%d, %d, %d);\n\n",cname,r,g,b); > } > fclose(rgb); > fclose(color); > } > > It will not break out of the while loop when the EOF is reached, > instead, it continues writing to the output file forever, or > until the disk fills up. :) However, the following code works > as expected: > > #include > void main(){ > > int r,g,b,result; > char cname[80]; > > FILE *rgb,*color; > > rgb=fopen("rgb.txt","r+"); > color=fopen("XColor.java","w+"); > while((result=fscanf(rgb,"%d%d%d%s",&r,&g,&b,cname))!=0){ > fprintf(color," ////////////////////\n"); > fprintf(color," // The color %s\n",cname); > fprintf(color," ////////////////////\n\n"); > fprintf(color," public final static Color %s = new\ > Color(%d, %d, %d);\n\n",cname,r,g,b); > } > fclose(rgb); > fclose(color); > } > > I'm confused by this behavior, the ANSI C standard denotes that > feof() returns non-zero if and only if the end of file is > reached. Yet even if I put while((feof(rgb))==0){, it still > insists that it has not reached the EOF. > > Any comments and/or input on this would be appreciated. > > ---russ I think the problem doesn't come from feof(). The first program is broken : it doesn't take care that fscanf() may fail (when there are, for example, two names for the same colours, it loops). The second program is wrong too : fscanf may stop when it reaches the EOF, but when it reaches an expression it doesn't understand too (for example two names). The answer is: if fscanf() returns 0, find the begin of the next line or an EOF. Something like: while (fscanf(...) != number_of_conversions) { /* Here number_of_conversions = 4 */ int c; while (((c = fgetc(rgb)) != EOF) && (c != '\n')) ; if (c == EOF) break; } Best regards. Guilhem. -- -----------------------------------| | /-\ LAVAUX Yves & Guilhem | | / \ /-\ | |-/ \ / \ /------------| | \ / \-/ | | \-/ ylavaux@pratique.fr | -----------------------------------| --------------1663BDF71480945943551305 Content-Type: text/plain; charset=us-ascii; name="xcolor.c" Content-Transfer-Encoding: 7bit Content-Disposition: inline; filename="xcolor.c" #include void main(){ int r,g,b,result; char cname[80]; FILE *rgb,*color; rgb=fopen("rgb.txt","r+"); color=fopen("XColor.java","w+"); while(!feof(rgb)){ while ((result = fscanf(rgb,"%d%d%d%s",&r,&g,&b,cname)) != 4) { int c; while ((c = fgetc(rgb)) != EOF && c != '\n') ; if (c == EOF) break; } fprintf(color," ////////////////////\n"); fprintf(color," // The color %s\n",cname); fprintf(color," ////////////////////\n\n"); fprintf(color," public final static Color %s = newColor(%d, %d, %d);\n\n",cname,r,g,b); } fclose(rgb); fclose(color); } --------------1663BDF71480945943551305--