Re: Behavior of feof()

Guilhem Lavaux (ylavaux@pratique.fr)
Sat, 22 Feb 1997 19:00:14 +0100


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 <stdio.h>
> 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 <stdio.h>
> 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 <stdio.h> 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--