Hi, I'm having trouble storing information from a text file into an array. I have the code working fine if it stores into non-array variables, but as soon as I try to use an array the program stalls. Can somebody point out the issue?
Code that works without array
Code:
FILE *fp = fopen("students.txt", "r");
int stu;
char name[30];
double t1, t2, a, labs, exams;
fscanf(fp, "%d,%lf,%lf,%lf,%lf,%lf,%[A-Za-z /]", &stu, &t1, &t2, &a, &labs, &exams, name);
printf("%d,%s,%lf,%lf,%lf,%lf,%lf", stu, name, t1, t2, a, labs, exams);
Code that fails with array
Code:
FILE *fp;
fp = fopen("students.txt", "r");
int stu[MAX_SIZE], i = 0;
char name[MAX_SIZE][20];
double t1[MAX_SIZE], t2[MAX_SIZE], a[MAX_SIZE],
labs[MAX_SIZE], exams[MAX_SIZE];
fscanf(fp, "%d,%lf,%lf,%lf,%lf,%lf,%[A-Za-z /]", stu[i], t1[i], t2[i], a[i], labs[i], exams[i], name[i]);
printf("%d %s %lf %lf %lf %lf %lf", stu[i], name[i], stu[i], t1[i], t2[i], labs[i], exams[i]);
The file is structured in this format:
Code:
00101010,80.0,79.0,88.0,67.0,90.0,Bob Newhart
00111010,88.0,77.0,66.0,57.0,56.0,Alex Trebek
00121010,80.0,79.0,88.0,67.0,90.0,Stephen Harper
00131010,88.0,77.0,66.0,57.0,56.0,Alfred Newman