Reputation: 11
My program isn't reading past the first line of my file. I have the file
John 22 67
Rickard 31 100
Andrew 21 34
Sarah 20 80
Attempting to read with:
void loadPeople(char fileName[],Person people[],int * length){
FILE *fptr = fopen("fileName", "r");
int i;
for(i=0; i<N; i++){
fscanf(fptr, "%s", people[i].name);
fscanf(fptr, "%d", &people[i].age);
fscanf(fptr, "%lf", &people[i].score);
fclose(fptr);
}
return;
}
The program is only reading the first line of the file into person[0] and not beyond.
Upvotes: 1
Views: 172
Reputation: 22158
You are closing the file after reading the first record. Move fclose()
out of the loop.
You should also check that the file was opened successfully, by checking that fptr
is not NULL
before looping through the file.
Upvotes: 3