Reputation: 605
there is the following function:
void readAndPrint(FILE * f) {
int c;
while(c = fgetc(f) != EOF) {
printf("%d", c);
}
}
In the main() body I used the following code to use the above function:
FILE * pFile;
pFile=fopen ("myfile.txt","r");
readAndPrint(pFile)
;
Whatever I put into myfile.txt, the program prints out ones. For example, for abc, 111 is printed out.
I know that c in the function should be declared int to properly compare it to EOF. Also, I expected an int code from the ASCII set for each char in the text file to be printed out (97 for a, ...). I cannot figure out why it prints out 'ones'... Does you know the reason why? Thank you in advance.
Upvotes: 3
Views: 3126
Reputation: 13424
(c = fgetc(f) != EOF)
- Here first fgetc(f) != EOF
this condition is happening and the result 1
or 0
is assigned to c
. Always a condition check returns TRUE
(1
) or FALSE
(0
).
Do while((c = fgetc(f)) != EOF)
Upvotes: 13