I'm working on some code to read a file that contains a list of users, their information, and put the information of all the users into a linked list. Two parameters of the user struct is a pointer to a linked list of locals, and a pointer to a linked list of points of interest.
Each line of the file is a different user.
The program is fully working for everything - except for the linked list of locals.
My process is to create a linked list of locals for each line of the file, and then pass the pointer to the first node of that list (together of the rest of the information from the user of that line) to the function insertpersonlist
to add that user and it's information to the linked list.
Each linked list of locals is working fine BEFORE I save it together with the rest of the user information into the linked list of users. After passing it to the function insertpersonlist
, I get segmentation fault when trying to access the list of locals.
I know the code is a bit big, but any help is really appreciated . Thanks in advance.
typedef struct pdi{
char *name;
char *descript;
char *schedule;
int id;
int popp;
int hotpoints;
struct pdi *prox;
}t_pdi;
typedef struct local{
int id;
char *name;
int popl;
struct local *prox;
}t_local;
typedef struct pessoa{
char *name;
char *address;
char *dob;
int phonenumber;
t_pdi *pdis;
t_local *local;
struct pessoa *prox;
}t_person;
void crialistapessoas(t_person *header_p, t_pdi *header_pdi, t_local *header_l){
FILE *fp;
t_person *node = header_p;
t_pdi *new1listpdi = create_headerPDI();
t_pdi *nodepdi = new1listpdi;
t_local *new1L;
t_pdi *new1PDI;
t_local *new1listlocals = create_headerL();
t_local *nodelocal = new1listlocals;
t_local *tempnodel;
t_pdi *tempnodepdi;
char *name = malloc(200*sizeof(char));
char *line = malloc(200*sizeof(char));
char *address = malloc(200*sizeof(char));
char *dob = malloc(200*sizeof(char));
int phonenumber;
int tempid,i, n;
fp = fopen("names.txt", "r");
fgets(line, 150, fp);
while(strcmp(line, "end")!=0){
name = strtok(line, "/");
address = strtok(NULL, "/");
dob = strtok(NULL, "/");
phonenumber = atoi(strtok(NULL, "/"));
new1listpdi = create_headerPDI();
new1listlocals = create_headerL();
nodepdi = new1listpdi;
nodelocal = new1listlocals;
n = atoi(strtok(NULL,"/"));
for(i=0;i<n;i++){
tempid = atoi(strtok(NULL,"/"));
printf("%d\n", tempid);
if(tempid>1000){
tempnodel = header_l;
while(tempnodel!=NULL && tempnodel->id != tempid){
tempnodel = tempnodel->next;
}
new1L = create_headerL();
new1L->id=tempid;
printf("%d\n", tempnodel->id);
new1L->name = strdup(tempnodel->name);
new1L->popl = tempnodel->popl;
new1L->next = nodelocal->next;
nodelocal->next = new1L;
}
else{
tempnodepdi = header_pdi;
while(tempnodepdi!=NULL && tempnodepdi->id != tempid){
tempnodepdi = tempnodepdi->next;
}
new1PDI = create_headerPDI();
new1PDI->id = tempid;
new1PDI->name = strdup(tempnodepdi->name);
new1PDI->descript = strdup(tempnodepdi->descript);
new1PDI->schedule = strdup(tempnodepdi->schedule);
new1PDI->hotpoints = tempnodepdi->hotpoints;
new1PDI->popp = tempnodepdi->popp;
new1PDI->next = nodepdi->next;
nodepdi->next = new1PDI;
}
}
insertpersonlist(header_p, name, address, dob, phonenumber, new1listlocals, new1listpdi);
fgets(line, 150, fp);
}
}
t_person *insertpersonlist(t_person *header_p, char *name, char *address, char *dob, int phonenumber, t_local *listlocals, t_pdi *listpdis){
t_person *new1;
t_person *node = header_p;
new1 = create_headerPerson();
if (new1== NULL)
return NULL;
while((node->next!=NULL)&&(strcmp(node->next->name, name)<0)){
node = node->next;
}
new1->name = strdup(name);
new1->address = strdup(address);
new1->dob = strdup(dob);
new1->phonenumber = phonenumber;
new1->pdis = listpdis;
new1->local = listlocals;
new1->next = node->next;
node->next = new1;
}