4

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;
 } 
leppie
  • 115,091
  • 17
  • 196
  • 297
jimo
  • 430
  • 2
  • 7
  • 19

3 Answers3

5

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

Anshul
  • 1,416
  • 1
  • 16
  • 42
  • 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'. – jimo Apr 04 '15 at 15:45
  • You'll definitely need to use `strtok`, or parse the words some other way. As written, your code counts only one occurrence per line. – Jim Mischel Apr 07 '15 at 20:49
0

Either use variable student in the last printf() line or place your matched text in variable word and also check your if condition.

SSH
  • 1,609
  • 2
  • 22
  • 42
0

You should read (create a function) words from the file. And by words I mean array of non-whitespace characters surrounded by whitespaces such as blank space (but don't record the white spaces in the words list). Then search through the word list (or through the words on the fly) to look for the required word.

Hiro
  • 110
  • 1
  • 7