im having a problem, im trying to make a list of a list where inicioC refers to the first node of clients, and every node of clients will have a list of rentals, referred as inicioA. The thing is, i dont know how to save the first pointer of the rentals, like, the first node of clients will only be saved one time, but wont the first node of every rental be different?
this are the structs:
typedef struct _aluguer
{
int id, estado, diaI, mesI, anoI, diaE, mesE, anoE;
struct _aluguer *prox;
}aluguer, *pAluguer;
typedef struct _registo
{
int nif,nalu;
char nome[100];
struct _registo *prox;
pAluguer *inicioA;
}registo, *pRegisto;
and this is the code I use to extract the info from a file into the list of lists
pRegisto iniC(pRegisto inicioC, pAluguer inicioA)
{
FILE *c;
int j, nif, nalu, l=0;
char nome[100];
c = fopen("clientes.txt", "r"); // abrir ficheiro
if(c == NULL)
{
printf("Erro ao abrir ficheiro %s", "clientes.txt");
exit(0);
}
while(fscanf(c, "%d %d %s", &nif, &nalu, nome) == 3) //format of info
{
pRegisto novoC = malloc(sizeof(registo));
if(novoC == NULL)
{
printf("erro alocacao memoria\n");
return inicioC;
}
novoC -> prox = NULL;
pAluguer inicioA = NULL;
pRegisto aux = inicioC;
pRegisto p = NULL;
novoC->nif=nif;
novoC->nalu=nalu;
strcpy(novoC->nome, nome);
while(aux != NULL)
{
p = aux;
aux = aux->prox;
}
if( aux == inicioC)
{
inicioC=novoC;
}
else
{
p->prox = novoC;
}
for(j=0; j<novoC->nalu; j++) // repeat is equal to the number of rentals
{
l++;
pAluguer novoA = malloc(sizeof(aluguer));
if(novoA == NULL)
{
printf("erro alocacao memoria\n");
return inicioC;
}
novoA -> prox = NULL;
pAluguer aux = inicioA;
pAluguer p = NULL;
fscanf(c, "%d %d", &(novoA->id), &(novoA->estado));
if(novoA->estado == 0)
{
fscanf(c, " %d %d %d", &(novoA->diaI), &(novoA->mesI), &(novoA->anoI));
}
else
{
fscanf(c, " %d %d %d %d %d %d", &(novoA->diaI), &(novoA->mesI), &(novoA->anoI), &(novoA->diaE), &(novoA->mesE), &(novoA->anoE));
}
while(aux != NULL)
{
p = aux;
aux = aux->prox;
}
if( aux == inicioA)
{
inicioA=novoA;
}
else
{
p->prox = novoA;
}
}
}
fclose(c);
return inicioC;
}