I'm "working" in a school project that needs to use linked lists with chars* and some other variables. The thing is:
I'm using a sub-function to open, read and store the data in a List but can't access to the string on it, I mean, can see other infos just dont that one...
void main()
{
Palavra* P = lerPalavras();
printf ("%s", P->c); //Was just testing in this ...
}
The sub-funtion
Palavra* lerPalavras () //Just ignore what is going on here in the code, the problem is when I call it, within the function the output works just fine, but when I pass to main it's just not working at all. Thanks
{
char linha [50];
Palavra* P = NULL;
Palavra* pv = NULL;
FILE* ficheiro = fopen ("keywords.txt", "r");
if(ficheiro == NULL)
{
printf ("Abertura do Ficheiro 'keywords.txt' Falhou!");
return NULL;
}
while (fgets (linha, sizeof (linha),
{
if (linha [strlen(linha) - 1] != '\n')
linha [strlen(linha)] = '\0';
else
linha [strlen(linha) - 1] = '\0';
pv = CriaKW ();
pv->c = linha;
pv->tam = strlen (linha);
P = insertKW (P, pv);
}
fclose (ficheiro);
return (P);
}
The struct "Mensagem"
typedef struct KW
{
char* c;
struct KW* nseg;
int ID;
long int tam;
} Palavra;
The function "CriaKW"
Palavra* CriaKW ()
{
Palavra * pv = (Palavra*) malloc (sizeof (Palavra));
return (pv);
}
The function "insertKW"
Palavra* insertKW (Palavra* P, Palavra* pv)
{
pv->nseg = NULL;
if (P == NULL) //Ver se a Lista está vazia
return (pv);
Palavra* p = P; //Variável Auxiliar para não perder a cabeça da Lista
while (p->nseg != NULL) //Ver se a "caixa" tem seguinte
p = p->nseg; //Correr a Lista
p->nseg = pv;
return (P);
}