Fgetc always returns 1

the following function exists:

void readAndPrint(FILE * f) {
    int c;

    while(c = fgetc(f) != EOF) {
        printf("%d", c);
    }
}

      

Basically () I used the following code to use the specified function:

FILE * pFile;

pFile=fopen ("myfile.txt","r");

readAndPrint(pFile)

      

;

No matter what I put in myfile.txt, the program prints them. For example, for abc, 111 is printed.

I know that the c in the function must be declared int in order to compare it correctly with EOF. Also, I expected the int from the ASCII set for each char to be printed in a text file (97 for a, ...). I can't figure out why it prints "those" ... Do you know why? Thank you in advance.

+3


source to share


1 answer


(c = fgetc(f) != EOF)

- Here the first fgetc(f) != EOF

this condition occurs and the result is 1

either 0

assigned c

. Always checking the status returns TRUE

( 1

) or FALSE

( 0

).



Do while((c = fgetc(f)) != EOF)

+9


source







All Articles