I'm trying to copy the words of a file .txt
into a linked list. I wrote something but what happens is that the program closes in a moment, as if there was an error in the assignment of the memory. I do not understand where I'm wrong.
#include <stdio.h>
#include <stdlib.h>
struct s_nodo
{
char* palabra;
struct s_nodo*sig;
};
typedef struct s_nodo* t_nodo;
void agregar (t_nodo*,char*);
void imprimir(t_nodo);
int main()
{
char aux[30];
t_nodo lista=NULL;
FILE*fd;
fd=fopen("c:\texto.txt","r");
while(!feof(fd))
{
fscanf(fd,"%s",aux);
agregar(&lista,aux);
}
imprimir(lista);
return 0;
}
void agregar (t_nodo*lista,char *aux)
{
if(*lista==NULL)
{
*lista=malloc(sizeof(t_nodo));
(*lista)->palabra=malloc((strlen(aux+1))*sizeof(char));
strcpy((*lista)->palabra,aux);
(*lista)->sig=NULL;
}
else agregar(&(*lista)->sig,aux);
}
void imprimir (t_nodo lista)
{
if(lista!=NULL)
{
printf("-%s-",lista->palabra);
imprimir(lista->sig);
}
}