When I read the file it doesn’t include the last record, so in this case Bill Biking does not show up
Here is the hobbies.dat file:
Bob Running
Josh Swimming
Bill Biking
And here’s the program’s code:
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
FILE *fp;
char name[30];
char hobby[30];
char *filename = "hobbies.dat";
fp = fopen(filename, "r");
if (fp == NULL) {
printf("File cannot open");
perror("The following error has occured");
}
else
{
printf("\nName\tHobby\n\n");
fscanf(fp, "%s%s", name, hobby);
while (!feof(fp))
{
printf("%s\t%s\n", name, hobby);
fscanf(fp, "%s%s", name, hobby);
}
}
system("pause");
return 0;
}
Change the loop as follows:
I changed this because you need to do your final print before you
fscanfagain and reach EOF.