我已经编写了下面的代码,但是 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);
最佳答案
不要使用 strcmp(),试试这个:
if (strstr(line, word)) { ... }
关于C语言无法让strcmp工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40918807/