I have spent a couple hours trying to figure out what was wrong with this code. I've tried using putting the code in a feof while loop, as well as taking the fscanf out of the loop so that it only runs once. These changes still provoke the segmentation fault, even though the data in the files are valid.
struct student *temp = (ident*) malloc (sizeof(ident));
while(fscanf(file1, "%s %s %d %f", temp->fname, temp->lname, temp->id, temp->gpa) != EOF) {
if(head == NULL)
head = temp;
else {
struct student *traverse = head;
while(traverse->next != NULL)
traverse = traverse->next;
traverse->next = temp;
printf("added");
}
}
The following is the struct:
struct student{
char fname[256];
char lname[256];
unsigned int id;
float gpa;
struct student *next;
};
An example of a line on the text file:
john doe 1 3.6
john smith 3 2.4