Search code examples
clinked-listfile-read

insert string read from a file to a linked list


LIST *list;
list = createList();
FILE *file = fopen("test.txt","r");
char line[50];
char* token;

while(fgets(line,sizeof(line),file))
{
    token = strtok(line," ,:=");
    while (token != NULL)
    {
       printf("\n%s",token);
       token = strtok(NULL," ,:=");
    }
}

this piece of code separates the lines in my file into tokens correctly. now , I want to insert them into a linked list. But adding addNode function inside the while loop :

while (tp != NULL)
{
     printf ("\n%s",token);
     token = strtok (NULL, " ,:=");
     addNode(li,&token);
}

does not work while inserting.

the addNode function is : (from the given library)

int addNode (LIST* pList, void* dataInPtr)
{
    bool found;
    bool success;
    NODE* pPre;
    NODE* pLoc;

    found = _search (pList, &pPre, &pLoc, dataInPtr);
    if (found)
       return (+1);

    success = _insert (pList, pPre, dataInPtr);
    if (!success)
       return (-1);
    return (0);
} 

Anyone has an idea about this?


Solution

  • This is possibly the problem:

     addNode(li,&token); /* Passing char**, not char* */
    

    change to:

     addNode(li,token);