• C

Reading a file as and comparing data to a list of known words

I was just wondering if anyone could help me with scanf and strcmp.  What I have to do is read words from a file one a time and compare teh words to a list of words if and if the word I read from the file is one of the words in my predefined list them I increase its counter.  I was just wondering if someone could give a short example of how to use scaf and strcmp in a situation similar to this.  I would appreciate it thanks.
Who is Participating?

Improve company productivity with a Business Account.Sign Up

rbrConnect With a Mentor Commented:
#define WORDS 3

char *list_of_words[WORDS]={"first","second","third");

int counter[WORDS];
char buf[1000];
int i;
FILE *fp;

fp=fopen ("myfile","r");

while (EOF != fscanf (fp,"%s",buf)) {
      for (i=0;i<WORDS;i++) {
            if (strcmp (buf,list_of_words[i])==0)
fclose (fp);

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.