I really need some help to figure out what's the problem with the reading from file of a nested lists. Briefly, these are the nested lists:
typedef struct car* parcheggio;
struct car{
char* targa;
char* nome;
char* cognome;
parcheggio next;
};
typedef struct data* calendario;
struct data{
char* data;
parcheggio aut;
calendario next;
};
And with the following function, passing the path of the txt file where all the dates are saved, I'm able to store dates from file into list:
calendario get_datefromFile(char folder[100]){
FILE *fp1;
calendario temp, temp1;
char flag;
fp1 = fopen(folder, "r");
temp = (calendario)malloc(sizeof(struct data));
temp->data = malloc(10*sizeof(char));
fscanf(fp1, "%s", temp->data);
temp1 = temp;
while(flag != EOF){
temp1->next = (calendario)malloc(sizeof(struct data));
temp1 = temp1->next;
temp1->data = malloc(10*sizeof(char));
fscanf(fp1, "%s", temp1->data);
flag = getc(fp1);
}
temp1->next = NULL;
fclose(fp1);
return temp;
}
Then I have other files, every file contains the info of struct car( targa nome and cognome) of a specific date, and obviously I want to match each calendario->data with data->aut and every data->aut->targa, ...->nome, ...->cognome. So to create a complete list of every date.
And I tried to do so through these functions:
calendario getcarall(calendario head1){
parcheggio be;
calendario temp = head1;
char dest[100] = "/home/utente/Documenti/Progetto/store/";
char path[100];
strcpy(path, dest);
while(temp != NULL){
strcat(dest, head1->data);
be = NULL;
be = car_filetolist(dest);
head1->aut = be;
strcpy(dest, path);
head1 = head1->next;
}
return temp;
}
parcheggio car_filetolist(char dest[100]){
FILE *file;
char flag;
parcheggio head, current;
file = fopen(dest, "r");
head = (parcheggio)malloc(sizeof(struct car));
head->targa = malloc(10*sizeof(char));
fscanf(file, "%s", head->targa);
head->nome = malloc(12*sizeof(char));
fscanf(file, "%s", head->nome);
head->cognome = malloc(12*sizeof(char));
fscanf(file, "%s", head->cognome);
current = head;
while(flag != EOF){
current->next = (parcheggio)malloc(sizeof(struct car));
current = current->next;
current->targa = malloc(10*sizeof(char));
fscanf(file, "%s", current->targa);
current->nome = malloc(12*sizeof(char));
fscanf(file, "%s", current->nome);
current->cognome = malloc(12*sizeof(char));
fscanf(file, "%s", current->cognome);
flag = getc(file);
}
current->next = NULL;
fclose(file);
return head;
}
But gcc returns segmentation fault when program enter in func car_filetolist.