我已经写下了下面的代码,但是strcmp
函数工作不正常。它不从文本文件中提取已知文本,并返回0
作为单词计数。C语言不能让Strcmp工作
int count = 0;
char line[400];
char word[20];
printf("Search for? \n");
scanf_s("%s", word, 19);
if (Fp == NULL)
{
printf("File not found");
}
while (!feof(Fp))
{
fgets(line, 150, Fp);
if (strcmp(line, word) == 0) //searches the line to find word
{
count++;//increment
}
}
printf("Search returned %s was found %d number of times", word, count);
注意'line'包含一个换行符。并阅读[为什么是“while(!feof(file))”总是错?](http://stackoverflow.com/questions/5431941/why-is-while-feof-file-always-wrong) – BLUEPIXY