C语言不能让Strcmp工作

时间:2016-12-01 19:25:28

标签: c strcmp

我已编写下面的代码,但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);

2 个答案:

答案 0 :(得分:3)

不要使用strcmp(),请改为尝试:

if (strstr(line, word)) { ... } 

答案 1 :(得分:1)

好吧,strcmp()没有按照你的想象去做。您的代码注释表示它会搜索该行中的单词;那是不对的。 strcmp只是表明你传递的两个字符串是否相同(或者,如果不是,那么它将在排序中排在第一位)。