I'm trying to import information from a file to a struct, but when I'm using malloc the program stop running. I've done similar functions before, I don't know why is not working.
This is my reading file function:
void ler_fich_salas(List_sala sala)
{
FILE *fich;
List_sala l;
char linha[10];
fich = fopen("fich_salas.txt","r");
l = l->next;
if (fich == NULL)
{
return;
}
else
{
/*ou l=l->next*/
while (!feof(fich))
{
printf("A");
fgets(linha, 10, fich);
printf("Z");
printf("%s",linha);/*testar se le bem no fich*/
printf("B");
free(l->nome_sala);
l->nome_sala = (char *)malloc(TAM*sizeof(char));
printf("C");
strcpy(l->nome_sala, strtok(linha,"\n"));
printf("D");
l = l->next;
}
}
fclose(fich);
}
and this is my struct:
typedef struct Sala_node *List_sala;
typedef struct Sala_node
{
char *nome_sala;
List_sala next;
}Cada_sala;
Any help would be appreciated! Thanks in advance.