I've been working on this for a few days now, and I'm seriously stuck on this function. For this part, I'm supposed to read in a file and create a linked list from the data in the file. I have my program reading in the files, no problem. What I am running into is when it tries to insert the data into the list, the program just crashes. Can someone maybe explain what I am doing wrong?
Here is the code to load the file (it is in my main):
Code:else if(choice == 5) { printf("Enter load file name.\n"); scanf("%s", &name2); fload = fopen(name2, "r"); while(!feof(fload)) { fscanf(fload, "%s %s %d %s %s", make, model, &year, color, license); printf("%s %s %d %s %s \n", make, model, year, color, license); //To make sure the file is being read properly if(insert(&head, make, model, year, color, license)) { printf("file loaded\n"); } } printf("File loaded!\n"); fclose(fload); }
And here is my insert function (which will work with the rest of the program):
Code:int insert(struct vehicle** head, char* make, char* model, int year, char* color, char* license) { struct vehicle* newvehicle; struct vehicle* crnt = (*head); newvehicle = malloc(sizeof(struct vehicle)); newvehicle->make = (char*)calloc(strlen(make)+1, sizeof(char)); newvehicle->model = (char*)calloc(strlen(model)+1, sizeof(char)); newvehicle->color = (char*)calloc(strlen(color)+1, sizeof(char)); newvehicle->license = (char*)calloc(strlen(license)+1, sizeof(char)); strcpy(newvehicle->make,make); strcpy(newvehicle->model,model); newvehicle->year = year; strcpy(newvehicle->color,color); strcpy(newvehicle->license,license); if ((*head) == NULL || (strcmp((*head)->license , license) > 0)) { newvehicle->next = (*head); (*head) = newvehicle; return 1; } while(crnt->next != NULL && (strcmp(crnt->next->license , license) < 0)) { crnt = crnt->next; } if(strcmp(crnt->next->license, license) == 0) return 0; newvehicle->next = crnt->next; crnt->next = newvehicle; return 1; }



LinkBack URL
About LinkBacks


