C语言 错误,缺少终止字符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16388118/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
error, missing terminating character
提问by monkey doodle
I have this missing terminating character, but I'm not sure what it wants or needs. I believe it's inside its inside the while second while loop. what can be done to fix this. I'm trying to input a text file with a string of paths. And then from there I parse through this string and put it to a link list. and then create a search function afterwards.
我有这个缺少的终止字符,但我不确定它想要或需要什么。我相信它在 while 第二个 while 循环中。可以做些什么来解决这个问题。我正在尝试输入一个带有路径字符串的文本文件。然后从那里我解析这个字符串并将其放入链接列表。然后创建一个搜索功能。
text file: path.txt
文本文件: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
code:
代码:
#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;
}
error:
错误:
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
回答by simonc
In the line
在行
char *token = strtok(inputstr, "\");
you need to use "\\"to specify a single backslash character.
您需要使用"\\"来指定单个反斜杠字符。
A single backslash is treated as the start of a multi-character escape sequence.
单个反斜杠被视为多字符转义序列的开始。

