因此,我有一些代码可以将文件中的字符串与用户输入进行比较,该输入已经过错误检查,以删除空格。
char text[100];
int c = 0;
int d = 0;
char string[100];
void space(void);
int main(/*int argc,char *argv[]*/)
{
int count = 1;
char wd[20], word[20];
FILE *fp;
fp = fopen("Student Usernames.txt", "r");
if (fp == NULL)
{
printf("given file doesn't exist");
getch();
} else
{
printf("Enter the word to search: ");
gets(text);
space();
fscanf(fp, "%s", wd);
while (!feof(fp))
{
if (strcmp(string, wd) == 0)
{
printf("%s found in the file. the given word is the %d word in the file",
word, count);
count = 0;
break;
} else
{
fscanf(fp, "%s", wd);
count++;
}
}
if (count != 0)
{
printf("given word is not found in the file");
}
getch();
}
}
void space(void) //Prototype stage, w/o semi colon
{
while (text[c] != '\0')
{
if (!(text[c] == ' ' && text[c] == ' '))
{
string[d] = text[c];
d++;
}
c++;
}
string[d] = '\0';
c = 0;
d = 0;
getch();
}然而,有一个小问题,当我继续显示代码时,它会给出一些随机垃圾作为文件中的单词,
例如:输入:A p p l e。程序将A p p l e变成Apple。输出:%^&$£$%^&
有人知道哪里出问题了吗?编辑:添加文本。
发布于 2014-02-05 12:19:23
printf("%s found in the file. the given word is the %d word in the file",
word, count);在上面打印中,如果您打印的是在代码中未赋值的word,而只是打印wd字符串。
发布于 2014-02-06 00:49:39
哈哈哈,原来输出应该是字符串,而不是单词,问题解决了
https://stackoverflow.com/questions/21565933
复制相似问题