I'm trying to read some data from a file then print it out but My code is only reading the first content and then gets stuck in an infinite loop (in the while loop). What am I doing wrong? My output is just Student: Abby GPA: 3 I'm using Visual Studio 2012. I am just following an example from my book.
//My data is Abbie 3.4 Oakley 3.5 Sylvia 3.6 Uwe 3.7 Ken 3.8 Aaron 3.9 Fabien 4
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
void main()
{
unsigned int GPA;//GPA
char student[10];//Student
FILE * cfPter;
//Check if file opened
if((cfPter = fopen("data.txt", "r")) ==NULL)
{
puts("File could not be opened");
}
//Read Contents
else
{
puts("Contents of file:\n");
fscanf(cfPter,"%s %f ", student, &GPA);
}
//While not at end Print the contents read
while(!feof(cfPter))
{
printf("Student: %s GPA: %f",student,GPA);
fscanf(cfPter, "%s %f", student, GPA);
//system("pause");
}
fclose(cfPter);
system("pause");
} //end main