我已编写下面的代码,但strcmp
功能无法正常工作。它没有从文本文件中提取已知文本,并返回0
字数。
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);
答案 0 :(得分:3)
不要使用strcmp(),请改为尝试:
if (strstr(line, word)) { ... }
答案 1 :(得分:1)
好吧,strcmp()
没有按照你的想象去做。您的代码注释表示它会搜索该行中的单词;那是不对的。 strcmp
只是表明你传递的两个字符串是否相同(或者,如果不是,那么它将在排序中排在第一位)。