将从文件中读取的字符串插入到链接列表中

时间:2011-12-04 12:13:49

标签: c linked-list file-read

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," ,:=");
    }
}

这段代码正确地将我文件中的行分隔为令牌。 现在,我想将它们插入到链表中。但是在while循环中添加addNode函数:

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

不起作用。

addNode函数是:(来自给定的库)

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);
} 

有人对此有所了解吗?

1 个答案:

答案 0 :(得分:1)

这可能是问题所在:

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

更改为:

 addNode(li,token);
相关问题