如何在C中的另一个文件中搜索文件中的一个单词?

如何在C中的另一个文件中搜索文件中的一个单词?,c,C,我有两个文件,第一个是我的主文件,另一个是ignoreList,其中包含禁止使用的单词。我想扫描主文件中的所有单词,并在ignoreList中搜索并打印未被禁止的单词。顺便说一下,在ignoreList中,禁用的单词会被逐行替换。但是,有一个问题,它不打印任何单词,只打印数字。这是我的密码 #include<stdio.h> #include<string.h> #include <stdlib.h> int banList(char a[20]); in

我有两个文件,第一个是我的主文件,另一个是ignoreList,其中包含禁止使用的单词。我想扫描主文件中的所有单词,并在ignoreList中搜索并打印未被禁止的单词。顺便说一下,在ignoreList中,禁用的单词会被逐行替换。但是,有一个问题,它不打印任何单词,只打印数字。这是我的密码

#include<stdio.h>
#include<string.h>
#include <stdlib.h> 

int banList(char a[20]);
int main() {

    char word[20];
    FILE *mainFile;

    if ((mainFile = fopen("file1.html", "r")) == NULL) {
        printf("Error! File does not exist.");
    }

    while( fscanf(mainFile, "%s", word) != EOF ) {

        if(banList(word) == 1) {

        }
        else {
            printf("%s\n", &word);
        }
    }

    fclose(mainFile);
    return 0;
    }

int banList(char a[20]) {

    char ban[20];
    FILE *ignoreList;

    if ((ignoreList= fopen("file2.txt", "r")) == NULL) {
        printf("Error! IgnoreList file does not exist to compare.");
    }

    while( fscanf(ignoreList, "%s", ban) != EOF ) {

        if (strcmp(a,ban)==1) {
            fclose(ignoreList);
            return 1;
        }
    }

    fclose(ignoreList);
    return 0;

}
忽略列表=

a
ain't
am
an
and
are
aren't
as
at
be
been
by
...

问题是您正在打印地址,因为您在声明时没有指定指针。所以它打印地址而不是数据

错:

printf("%s\n", &word);
正确的一点:

 printf("%s\n", word);
strcmp()

如果返回值<0,则表示a小于ban

如果返回值>0,则表示ban小于a

如果返回值=0,则表示a等于ban

你有

if(strcmp(a,ban)==1) //means doen't match
使用


在返回之前,请关闭文件ignorelist@jasinthpremkumar我试过了,但没用。这只是一个建议,不是解决办法:)别担心,我们会找到的out@bluesshead您得到的输出是什么?请显示完整的代码(没有
#include
库头)和一个小样本的禁用单词文件和主文件,每行说3行。非常感谢!别客气
if(strcmp(a,ban)==1) //means doen't match
if(strcmp(a,ban)==0)