how to search in a file in c

Solutions on MaxInterview for how to search in a file in c by the best coders in the world

showing results for - "how to search in a file in c"
Arianna
21 Nov 2017
1#include <stdio.h>
2#include <string.h>
3#include <stdlib.h>
4
5int main(int argc, char const *argv[])
6{
7int num =0;
8char word[2000];
9char *string[50];
10
11FILE *in_file = fopen("student.txt", "r");
12//FILE *out_file = fopen("output.txt", "w");
13
14if (in_file == NULL)
15{
16    printf("Error file missing\n");
17    exit(-1);
18}
19
20while(student[0]!= '0')
21{
22    printf("please enter a word(enter 0 to end)\n");
23    scanf("%s", student);
24
25
26    while(!feof(in_file))
27    {
28        fscanf(in_file,"%s", string);
29        if(!strcmp(string, student))==0//if match found
30        num++;
31    }
32    printf("we found the word %s in the file %d times\n",word,num );
33    num = 0;
34}
35
36return 0;
37 } 
38