1

how I can make my program read a text file and compare the words from the text file with the words that I defined in an array and print a message. I think the main problem is the for loop cause i'm not sure if it iterates correctly.Here is my code:

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;
    }
}
S.S. Anne
  • 15,171
  • 8
  • 38
  • 76
  • 4
    Welcome to Stack Overflow! [Why `while(!feof(file))` is always wrong](https://stackoverflow.com/questions/5431941/while-feof-file-is-always-wrong) – Barmar Nov 01 '19 at 17:48
  • 2
    `keywords[i]== x` should be `strcmp(keywords[i], x) == 0` – Iłya Bursov Nov 01 '19 at 17:49
  • 2
    What is this line for: `char x = gets(as_array);`? That's reading a line from standard input, and overwriting `as_array` with it. – Barmar Nov 01 '19 at 17:49
  • 1
    [Why is “while ( !feof (file) )” always wrong?](https://stackoverflow.com/questions/5431941/why-is-while-feof-file-always-wrong) – Antti Haapala -- Слава Україні Nov 01 '19 at 18:00
  • "Could not open file" is the canonical example of a bad error message. Why could the file not be opened? Was it a permission issue? Let the system give you a reason. `if ((yourfile = fopen(name, "r"))== NULL){ perror( name ) ...` – William Pursell Nov 01 '19 at 18:51
  • see also [Why is the gets function so dangerous that it should not be used?](https://stackoverflow.com/q/1694036/995714) – phuclv Nov 02 '19 at 00:51

1 Answers1

0

Assuming your text file is test.txt and contains

if
else
return
switch
case

Then this code works, sorry I reformated a bit, and the 0 shd be\o , and I hard coded the file name, but you'll get it, have fun.

#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;

    }
BobRun
  • 756
  • 5
  • 12