I am reading a list of grades from a txt file into an array. It worked fine when reading user input, but I'm having trouble reading each line when scanning from file. The number of students is variable. The number of grades per student is variable. I have no trouble reading the number of students and number of assignments, but when reading from file I'm having trouble pulling the int (grade) from each line for each student. The input may be like a or b (or any larger number of students/assignments):
txt-example1 (the comments including and after // are my own and not in txt file)
2 //number of students
3 //the number of grades per student (will match the number of grade rows below)
theo alvin //the number of names will match the number of students
75 60
89 90
79 95
txt-example2
3
4
theo alvin simon
78 85 90
85 96 76
77 99 100
88 55 92
I can put the names into 1 dimension of a 2d array (I'll use the second dimension later to print - no problems with that part). I want to get the grades into a 2d array. Here is what I have
#include<stdio.h>
#include<string.h>
int numStus;
int numGrades;
int main()
{
FILE* inputFile;
char stuNames[numStus][10];
int grades[numGrades][numStus];
inputFile = fopen("testData.txt","r"); //assume inputFile has data from Ex 1 or 2 above
fscanf(inputFile,"%d",&numStus);
fscanf(inputFile,"%d",&numGrades);
int i;
int j;
for (i=0; i<numStus; i++)
{
fscanf(inputFile,"%s",&stuNames[i]);
}
//here is where I'm having trouble
for(i=0;i<numGrades;i++)
{
for(j=0;j<numStus; j++)
{
//I want to use fscanf, but don't know how to account for carriage returns to iterate into next part of array
}
}
}
What worked when getting from user input:
int i;
int j;
int k;
for (i=0; i<numGrades; i++)
{
for (j=0; j<numStus; j++)
{
printf("Enter grade for Assignemnt %d for ",i)
for(k=0;k<10;k++)
{
printf("%c",stuNames[j][k]);
}
scanf("%d",&grades[i][j]);
}
}
The part immediately above worked well for user input grades. When getting the same input from a file I'm not sure how to get the grades into the proper dimensions. Any advice on how to account for the newline/CR to increment the array would be very much appreciated. Thanks.