I am writing a C program to search for some keyword in a text file
The following is the product, but how can I change the
char *string[] = {
"apple",
"orange",
"banana",
};
to become input from file?
(That means there will be one file storing keyword and the other one is for searching)
I tried for lots of time but I fail to do so...can anyone help?
Thanks:)
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main(void)
{
char *string[] = {
"apple",
"orange",
"banana",
};
FILE *fptr;
fptr=fopen("abc.txt","r");
int cnt=0, n;
char word[100];
while (fscanf(fptr, "%100s", word) != EOF)
{
size_t n;
for (n = 0; n < sizeof string / sizeof *string; ++n)
{
if (strcasecmp(word, string[n]) == 0)
++cnt;
}
}
printf("No. of word found: %d",cnt);
getchar();
}