fgetc() returns an int not a char (can cause EOF problems).

Flag is useless,


int ch;

/* ... */

while((ch = fgetc(fin)) != EOF)