fgets() reads from the file into a null-terminated string. The EOF is not stored. EOF is not even actually a character. It is an int so that its bit-pattern can be distinguished from all chars. The upshot of all this is that you need to check for a 0 instead of EOF.