Here is my code. I am doing some C practice for processing a file
I don't think the struct definition is the problem but I post it as well to give some context.
typedef struct carType Car;
struct carType {
int vehicleID;
char make[20];
char model[20];
int year;
int mileage;
double cost;
Car *next;
};
And the function that i think is causing the segmentation failure.
void TextLoad(Car *headPointer)
{
char fileName[20];
//prompt user for name of textfile to print to
scanf("%s", fileName);
FILE *fpt;
//estabish an IO connection
fpt = fopen(fileName, "r");
//current car to be printed
Car *current;
current = headPointer->next;
while(fscanf(fpt,"%d %s %s cost:$%f mileage:%d, vehicleID:%d",¤t->year,
current->make, current->model,¤t->cost,¤t->mileage, ¤t->vehicleID) != EOF)
{
current = current->next;
}
fclose(fpt);
}
The file that I tested this function had this content
2014 Toyota Celica cost:$90000 mileage:5000, vehicleID:1
2014 Toyota Rav4 cost:$4500 mileage:4000, vehicleID:2
What I have is basically a struct car and I want to use info from the file to initialize the fields of the struct car. Does anyone know where this segmentation failure is coming from? I checked on other threads fclose() causing segmentation fault, and Code fails with segmentation fault but I made sure to call fclose to close the IO connection and I didn't initialize another file pointer but that worked fine. I think the problem is the fscanf but I have the right format don't I?