How to search for files in C

How to search for files in C

C language search file example

/*
Example: search string in a file in C language
*/
#include <string.h>
#include <stdlib.h>
#include <stdio.h>

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

    FILE *in_file = fopen("test.txt", "r");
    if (in_file == NULL)
    {
        printf("Cannot open file, file does not exist?\n");
        exit(-1);
    }

    printf("please enter a word\n");
    scanf("%s", word);
    while ( fscanf(in_file,"%s", string) == 1)
    {
        if(strstr(string, word)!=0) { // found 
            num++;
        }
    }
    printf("find %  %d times in the file\n",word,num );
    num = 0;
    fclose(in_file);
    
    return 0;
}
Source:‮www‬.lautturi.com
Created Time:2017-08-29 08:55:09  Author:lautturi