You need to check the return value of fgets. If a read has been successful, fgets
returns the pointer to the buffer that you passed to it (i.e. string
in your example). If the End-of-File is encountered and no characters have been read, fgets
returns NULL
.
Try this:
char string[100];
while(fgets(string, 100, fp)) {
printf("%s\n", string);
}