【问题标题】:error in my search linked list implementation我的搜索链表实现中的错误
【发布时间】:2013-05-04 22:50:24
【问题描述】:

我的程序似乎没有正确打开文本文件。 我有一个 path.txt,它是我创建的文件夹和文本文件的所有路径的字符串表示形式。然而,在运行程序时,它不会输出用户要求的文本文件的 LINES。

输出

enter text file
warning: this program uses gets(), which is unsafe.
a1.txt

IT 应该有输出

This is a1

a1.txt 的文本:

This is a1

文本文件:path.txt/ 我的文件夹是这样设置文本文件的。

a/a1.txt
a/a2.txt
a/b/b3.txt
a/b/b4.txt
a/c/c4.txt
a/c/c5.txt
a/c/d/d6.txt
a/c/d/g
a/c/d/h
a/c/e/i/i7.txt
a/c/f/j/k/k8.txt

代码:

#include <string.h>
#include <stdio.h>
#include <stdlib.h>

typedef struct sMyPath{
        char *element;
        struct sMyPath *next;
} tMyPath;


int main(void)
{
        FILE *pFile;
        pFile = fopen("path.txt", "r");
        char inputstr[1024];
        tMyPath *curr, *first = NULL, *last = NULL;

//get the text file, and put it into a string inputstr

        if (pFile != NULL)
        {
                while(!feof(pFile))
                {
                        fgets(inputstr, sizeof(inputstr), pFile);
                }
        fclose(pFile);
        }
        else
        {
                printf("Could not open the file.\n");
        }

//使用token获取字符串的每一段 //单独的目录和文本文件,放到一个链接列表中

char *token = strtok(inputstr, "/");
while (token != NULL)
{
if(last == NULL){
        //creating node for directory
        first = last = malloc (sizeof (*first));
        first -> element = strdup (token);
        first -> next = NULL;
} else {
        last -> next = malloc (sizeof (*last));
        last = last -> next;
        last -> element = strdup (token);
        last -> next = NULL;
}
token = strtok(NULL, "/");
}

//向用户询问txt文件

char pathU[20];
printf("enter text file\n");
gets(pathU);

//检查文本文件是否存在,如果存在则输出整个文本文件,否则不存在

   while(first != NULL)
    {
            if(first -> element == pathU)
            {
                    FILE *nFile;
                    char texxt[300];
                    nFile = fopen(pathU, "r");
                    while (!feof(nFile))
                    {
                            fgets(texxt, 300, nFile);
                            printf("%s", texxt);
                    }

            }

            else if(first == NULL)
            {
                    printf("invalid file name\n");
            }

            else
            {
            first = first -> next;
            }

}
return 0;
}

【问题讨论】:

    标签: c file search linked-list


    【解决方案1】:

    我了解两种可能的要求/实现。

    1) 通过您的实现,每个链接节点将只包含文件名和目录名以及不是路径名。如果您需要存储整个路径名,请使用 '\n' 作为分隔符。

    char *token = strtok(inputstr, "\n");
    

    token = strtok(NULL, "\n");
    

    这假设,当您的输入是 a/a1.txt 时,您的当前目录包含目录 a,而该目录又包含文件 a1。 txt

    2) 否则,您现有的代码期望 a1.txt 位于当前目录中,尽管它与输入文件内容相矛盾。


    不管怎样,下面这段代码都是罪魁祸首,

    if(first -> element == pathU)
    

    比较指针而不是字符串。替换为,

     if( strcmp( first -> element, pathU ) == 0 )
    

    如果您的要求更明确,我可以提供更好的解决方案..

    【讨论】:

      【解决方案2】:

      问题似乎出在字符串比较中:first -&gt; element == pathU。在这里,您正在比较指针,而不是字符串的字符。改用strcmpif (strcmp(first -&gt; element, pathU) == 0) ...

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-04-15
        • 2020-01-03
        • 2015-01-18
        • 2022-12-15
        相关资源
        最近更新 更多