I have this program for reading char by char a file and print it out on the screen:
#include<stdio.h>
int main()
{
unsigned char mychar;
FILE *fp;
fp=fopen("test.txt", "r");
while((mychar = getc(fp))!=EOF)
printf("%c", mychar);
fclose(fp);
return 0;
}
It prints the file but then it continues to loop forever. Can you help me?
unsigned char mychar;
-->int mychar;
char
is wrong. Becausechar
issigned char
orunsigned char
.getc
. It returns anint
actually, and that is the type that should be used.