Passing data from file to arrays
I haven't been using files for very long, not sure if im using the correct functions here, but I keep getting 'segmentation fault', and I'm not sure where my problem might be.
Code:
#include<stdio.h>
/* Loads data from file into arrays */
void loadData(int stu[],char name[][20],double t1[],double t2[], double a[], double labs[], double exams[])
{
int i;
FILE *fp;
fp = fopen("student.txt", "r");
for (i = 0; i < 9; i++){
fscanf(fp, "%d,%c,%lf,%lf,%lf,%lf,%lf", stu[i], name[i], t1[i], t2[i], a[i], labs[i], exams[i]);
printf("%d,%c,%lf,%lf,%lf,%lf,%lf", stu[i], name[i], t1[i], t2[i], a[i], labs[i], exams[i]);
}
fclose(fp);
}
main ()
{
int stu[10];
double t1[10], t2[10], a[10], labs[10], exams[10];
char name[10][20];
loadData(stu, name, t1, t2, a, labs, exams);
}
Basically I just want to read into a file, and copy each field separated by commas into different arrays. Any help would be greatly appreciated!