I have a text file with the following data:
11111,First,Last,2,COR,100,SEG,200
22222,F,L,1,COR,100
33333,Name,Name,3,COR,100,SEG,200,CMP,300
***
I need to separate the data in each line (at the commas) and add each line of data into a linked list. Here is a piece of the code that I am using to do it:
struct courseInfo{
int courseID;
char courseName[30];
};
typedef struct courseInfo cInf;
struct studentInfo{
char studentID[8];
char firstN[20];
char lastN[25];
int nCourses;
cInf courseInf[10];
struct studentInfo *next;
};
typedef struct studentInfo sInf;
void loadStudentInfo(){
FILE *loadFile;
loadFile = fopen("studentRecords.txt", "r");
while(1){
sInf *loadStudent;
loadStudent = (sInf*)malloc(sizeof(sInf));
char Temp[256];
fgets(Temp,256,loadFile);
if (Temp[0] == '*')
break;
int commaCount = 0;
int i = 0;
while(Temp[i] != '\0'){
if(Temp[i] == ',' && commaCount == 0){
char stdID[8];
strcpy(stdID, Temp);
int commaLoc = 0;
while(stdID[commaLoc] != ','){ //Finds where stdID ends
commaLoc++;
}
for(;commaLoc < 8; commaLoc++){ //Delimits stdID, 8 is the max size of studentID
stdID[commaLoc] = '\0';
}
printf("%s\n", stdID);
strcpy(loadStudent ->studentID, stdID); //Causing segmentation fault
commaCount++;
}
Now, what I have done to separate the data in each line may not be the most efficient but I tried using strtok() (which read the first line fine but resulted in a segmentation fault when reading the second line) and also tried using fscanf and %[^,] but it did not work for me. Anyway, I don't think that the method I am using to separate the data is causing the segmentation fault. This loop will continue on separating and storing the rest of the data in the appropriate member of loadStudent. The line causing the segmentation fault is shown above. Any help with determining the cause of this error is appreciated.