c - 在C中的txt文件中搜索单词

标签 c file

我如何让我的程序读取一个文本文件并将文本文件中的单词与我在数组中定义的单词进行比较并打印一条消息。我认为主要问题是 for 循环,因为我不确定它是否正确迭代。这是我的代码:

define MAX_SIZE 1000

int main()
{
    FILE * yourfile;

    char  as_array[MAX_SIZE];
    char name[20];
    const char * keywords[]={"if", "else", "return", "switch", "case", "default", "for", "do", "while", 
                             "break", "continue", "struct", "typedef", "union", "enum", "sizeof", "int", "float", "double", 
                             "void", 
                             "extern",
                             "signed", "unsigned", "long", "short", "static", "const",  "goto", "auto", "register", "volatile"};
    printf("Please write the file you want to open: \n ");
    scanf("%s", name);

    int number_of_keywords = sizeof(keywords)/sizeof(keywords[0]);

    //fopen opens the file; exits with error if the file cannot be opened
    if ((yourfile = fopen(name, "r"))== NULL){
        printf("Could not open file: %s", name);
        exit(1);
    }
    else printf("Your file has been successfully opened!\n");

    while(!feof(yourfile)){
        fgets(as_array, MAX_SIZE, yourfile);
        printf("%s\n", as_array);
        char x = gets(as_array);

        for(int i = 0 ; i<number_of_keywords; ++i){
            if(keywords[i]== x){
                printf("I found word %s\n", keywords[i]);
            }
        }
        return 0;
    }
}

最佳答案

假设你的文本文件是test.txt并且包含

if
else
return
switch
case

然后这段代码就可以工作了,抱歉,我重新格式化了一下,0 shd 是\o ,我硬编码了文件名,但你会明白的,玩得开心。

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

#define MAX_SIZE 1000

    int main()
    {
        FILE * yourfile;
        int i;
        int number_of_keywords;
        char string[200];
        char  as_array[MAX_SIZE];
        char name[20]="test.txt";
        const char * keywords[]={"if", "else", "return", "switch", "case", "default", "for", "do", "while", 
                                 "break", "continue", "struct", "typedef", "union", "enum", "sizeof", "int", "float", "double", 
                                 "void", 
                                 "extern",
                                 "signed", "unsigned", "long", "short", "static", "const",  "goto", "auto", "register", "volatile"};
      //  printf("Please type file name to open: \n ");
      //  scanf("%s", name);

        number_of_keywords = sizeof(keywords)/sizeof(keywords[0]);

        //fopen opens the file; exits with error if the file cannot be opened
        if ((yourfile = fopen(name, "r"))== NULL){
            printf("Could not open file: %s", name);
            exit(1);
        }
        else printf("Your file has been successfully opened!\n");


      while(  fgets( string, 200 , yourfile))
      {
        // problem : fgets grabs \n
        // remove it
        string[strlen(string)-1]=0;    
        printf( string);
        getchar();
        for( i=0 ; i<number_of_keywords; i++)
        {
         if(!strcmp(keywords[i], string))
                    printf("I found word %s\n", keywords[i]);
        }
       } 

         fclose(yourfile);
            return 0;

    }

关于c - 在C中的txt文件中搜索单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58663618/

相关文章:

c - 未分配指针

C# - 在 USB FAST 上创建大文件

file - 如何使用groovy从目录中获取最新文件?

python - 需要帮助在同一目录中的文件夹中创建 txt 文件

c - 如何使用 wc_EccPublicKeyDecode 在 Wolfssl 中导入 der 证书

C: switch 语句错误: "this is the first entry overlapping that value"

c - 使用 trie.c 和 trie.h 打印 string-tri

c - 从C中的文件中读取特定字段

c# - 互连流

char 未在 while 循环中注册