4

我有这个缺失的终止字符,但我不确定它想要或需要什么。我相信它在第二个while循环内部。可以做些什么来解决这个问题。我正在尝试输入带有路径字符串的文本文件。然后从那里我解析这个字符串并将其放入链接列表。然后创建一个搜索功能。

文本文件: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");
        }

//using tokens to get each piece of the string
//seperate directories and text files, put it into a link list

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




//ask user for txt file
        char pathU[20];
        printf("enter text file\n");
        gets(pathU);


//check if text file exist, if yes output entires in text file, else say no
        while(first != NULL)
        {
                if(strcmp (first -> element,pathU)==0)
                {
                        FILE *nFile;
                        char texxt[300];
                        nFile = fopen(pathU, "r");
                        while (!feof(nFile))
                        {
                                fgets(texxt, 300, nFile);
                                printf("Theses are the contents\n");
                                printf("%s", texxt);
                        }

                }

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

                else
                {
                first = first -> next;
                }




        }

return 0;
}

错误:

search.c:36:33: warning: missing terminating " character
search.c: In function ‘main’:
search.c:36: error: missing terminating " character
search.c:37: error: expected expression before ‘while’
search.c:50:24: warning: missing terminating " character
search.c:50: error: missing terminating " character
search.c:95: error: expected declaration or statement at end of input
4

1 回答 1

13

在行

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

您需要使用"\\"来指定单个反斜杠字符。

单个反斜杠被视为多字符转义序列的开始。

于 2013-05-05T19:13:19.920 回答