Behavior of feof()

Russell Berry (berrex@albany.net)
Sat, 22 Feb 1997 01:25:40 -0500 (EST)


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

_______________________________
Berrex Computer Solutions
Turnkey Solutions/Consulting
Software Development
http://www.albany.net/~berrex
berrex@albany.net