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();
}

Show us what you've tried. Clearly you know how to open a file and read words from it (more or less), so what's wrong with declaring an array of strings and filling it up with the words from a file?

char keywords[100][100];
FILE *in = fopen ( "keywords", "r" );

if ( in != NULL ) {
  char word[100];
  int i;

  for ( i = 0; i < 100; i++ ) {
    if ( fscanf ( in, "%99s", word ) != 1 )
      break;

    strcpy ( keywords[i], word );
  }

  fclose ( in );
}

try out this one.i think this will work out for ur needs.

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

void main(void)
{

 char key[20][20];
    int cnt=0, n,n1=0;
    char word[100];
	FILE *fptr,*fptr1;
  fptr1=fopen("key.txt","r");
   fptr=fopen("abc1.txt","r");
while(fscanf(fptr1, "%s",key[n1] ) != EOF)
 {
 n1++;
   }
while (fscanf(fptr, "%100s", word) != EOF)
 {
    size_t n;

     for (n = 0; n < n1; ++n)
     {
	if (strcmp(word, key[n]) == 0)
	   ++cnt;
     }
 }

printf("No. of word found: %d",cnt);
getchar();
}

>try out this one.i think this will work out for ur needs.
It's customary not to solve the problem for people so that they can actually learn something by figuring it out. Ironically, your solution is actually worse off than the original code...

>void main(void)
main doesn't return void, it returns int. At best your program has implementation-defined behavior and at worst it has undefined behavior. Not a good start.

>fptr1=fopen("key.txt","r");
>fptr=fopen("abc1.txt","r");
You failed to check these operations for success. You also failed to close the files, which is a good idea.

>while(fscanf(fptr1, "%s",key[n1] ) != EOF)
1) You never check the value of n1. If the file has 5,000 words, your code will happily read them into memory that it shouldn't.

2) Please don't use EOF as the loop condition. Yes, fscanf will return EOF if it reaches end-of-file before converting anything. That means if you have more than one conversion specifier, it's possible that one will read successfully and the other will fail due to end-of-file and the loop won't stop. This is a bad practice because it only has limited applicability. A better practice is to use the expected number of converted values, which always treats partial reads as failure.

3) The %s specifier is evil. Never use it without a suitable field width or you're no better off than you are with gets.

>for (n = 0; n < n1; ++n)
While it's nice that you're trying by making n a size_t, n1 is an int, so you've got potential issues with comparing signed and unsigned values. Either make n1 a size_t, or n an int.

Thanks for pointing out the mistakes.But it wasn't as bad as you wrote about it.i do agree that some mistakes could have been avoided like mentioning width along with conversion characters,closing of file pointers,comparing between signed and unsigned.i do agree that main is recommended to return int under c99standard.but some systems like tuboc will allow it.

>But it wasn't as bad as you wrote about it.
Yes, yes it was.

>i do agree that main is recommended to return int under c99standard.
main has returned int since before the language was called C. void main has never been standard, and using it is either non-portable or undefined. When you could easily remove the problem, using void main is the height of stupidity. Stop being stubborn.

>but some systems like tuboc will allow it.
Ah, so you're clairvoyant? You know for a fact that the OP uses Turbo C or a "system like" it? You can use crap like void main in private, but expect to be constantly corrected by people like me if you try to encourage blatantly wrong C. Also expect to lose all credibility as a qualified helper, because it's generally accepted that if you use void main, you really should learn C before trying to teach it.

...But it wasn't as bad as you wrote about it.
....i do agree that main is recommended to return int under c99standard.but some systems like tuboc will allow it.

It is human to err, but is stupid to continue in the err.

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();
}

Below is a revised version of the code you posted. - Tim


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

int main(void)
{
char *string[] = { {"apple"}, {"orange"}, {"banana"}, };
FILE *fptr;
int cnt=0, n;
char word[100];
char *pszNewLine;
char **pszSrchWord = string;
int iNumSrchWords = sizeof(string) / sizeof(char *);

if (!(fptr = fopen("abc.txt", "r")))
{
printf("\nERROR: opening file - %s\n", strerror(errno));
return (errno);
}

while (fgets(word, sizeof(word)-1, fptr))
{
pszSrchWord = string;

if (pszNewLine = strchr(word, '\n'))
*pszNewLine = '\0'; // <-- lose the newline.

for (n = 0; n < iNumSrchWords && *pszSrchWord; n++)
{
if (!strcmp(word, *pszSrchWord))
{
++cnt;
break;
}
pszSrchWord++;
}
}
fclose(fptr);
printf("No. of word found: %d", cnt);
getchar();

return (0);
}

This article has been dead for over six months. Start a new discussion instead.