/* The program takes the book info file from the first example of
chapter 28; also reads each line from the file and outputs it to the
screen. */
#include <stdio.h>
#include <stdlib.h>
FILE * fptr;
main()
{
char fileLine[100]; // Will hold each line of input
fptr = fopen("C:\\users\\DeanWork\\Documents\\BookInfo.txt","r");
if (fptr != 0)
{
while (!feof(fptr))
{
fgets(fileLine, 100, fptr);
if (!feof(fptr))
{
puts(fileLine);
}
}
}
else
{
printf("\nError opening file.\n");
}
fclose(fptr); // Always close your files
return(0);
}
This is an example from the book C Programming Absolute Beginner's Guide (3rd Edition)
We already have while(!feof(fptr))
to check the file is not empty. Why do we still nedd if(!feof(fptr)
to check it again?