EOF
is an Integer with the value -1
.
When you do ch=fgetc(fp)
in the while loop, you read into an unsigned char
, that can by definition not be signed, so it can't be equal to -1
.
A solution could be to read into an integer and to cast it after having checked for EOF
.
int ch;
while(ch=fgetc(fp))
{
if(ch == EOF)
break;
//OTHER INSTRUCTIONS
}
Refer to this for a sample of how it should be implemented.