Search string in a file in c

22,010

Added a sample code in its simplest form. Take care of any corner cases. If you are searching a string "to". And file content is :

<tom took two tomatoes to make a curry> . 

The output would come as 5. But in actual there is only one word "to".

Code:

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

int main(int argc, char const *argv[])
{
        int num =0;
        char word[2000];
        char string[50];
        char student[100] = {0};

        while(student[0]!= '0')
        {
                FILE *in_file = fopen("student.txt", "r");
                if (in_file == NULL)
                {
                        printf("Error file missing\n");
                        exit(-1);
                }

                printf("please enter a word(enter 0 to end)\n");
                scanf("%s", student);
                while ( fscanf(in_file,"%s", string) == 1)
                {
                        //Add a for loop till strstr(string, student) does-not returns null. 
                        if(strstr(string, student)!=0) {//if match found
                                num++;
                        }
                }
                printf("we found the word %s in the file %d times\n",student,num );
                num = 0;
                fclose(in_file);
        }
        return 0;
}

As rightly said by my fellow colleagues we need to have one more loop to traverse for any further instance of same word in the same line.

Note: In case if you want the word "to" only to be counted, make sure to check the "string - 1" and "string + 1" character for all the possible word delimiters like space, comma, full stop, newline, Exclamatory, ampersand, equals and any other possibilities. One simple way would be to use strtok which would tokenize the buffer into words based on delimiters specified in the argument. Checkout how to use strtok.

http://www.tutorialspoint.com/c_standard_library/c_function_strtok.htm

Share:
22,010
jimo
Author by

jimo

Only he who has no use for the empire is fit to be entrusted with it.

Updated on July 21, 2021

Comments

  • jimo
    jimo almost 3 years

    I am trying to write a program that can search a string in a file (called student.txt). I want my program to print the word if it finds the same word in the file, but its showing error.

    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    int main(int argc, char const *argv[])
    {
    int num =0;
    char word[2000];
    char *string[50];
    
    FILE *in_file = fopen("student.txt", "r");
    //FILE *out_file = fopen("output.txt", "w");
    
    if (in_file == NULL)
    {
        printf("Error file missing\n");
        exit(-1);
    }
    
    while(student[0]!= '0')
    {
        printf("please enter a word(enter 0 to end)\n");
        scanf("%s", student);
    
    
        while(!feof(in_file))
        {
            fscanf(in_file,"%s", string);
            if(!strcmp(string, student))==0//if match found
            num++;
        }
        printf("we found the word %s in the file %d times\n",word,num );
        num = 0;
    }
    
    return 0;
     } 
    
    • Anshul
      Anshul about 9 years
      if(!strcmp(string, student))==0 should be replaced with if(!strcmp(string, student)==0)
    • jimo
      jimo about 9 years
      still getting errors
    • mushfek0001
      mushfek0001 about 9 years
      What kind of error are you getting exactly?
    • Anshul
      Anshul about 9 years
      are you looking for string search or word search. For example if you are searching a string "to". And file content is : <tom took two tomatoes to make a curry> . The output would come as 5. But in actual there is only one word "to"
  • jimo
    jimo about 9 years
    Thanks, but yea sorry to confuse stuff here, I am looking for the word. Like, the example you mentioned I want my program to find and print 'to'.
  • Jim Mischel
    Jim Mischel about 9 years
    You'll definitely need to use strtok, or parse the words some other way. As written, your code counts only one occurrence per line.