Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2000-04-11
1
Medium Priority
?
191 Views
Last Modified: 2010-04-15
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.
0
Comment
Question by:BCBob2001
1 Comment
 
LVL 10

Accepted Solution

by:
rbr earned 60 total points
ID: 2706525
#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)
                counter[i]++;
      }
}
fclose (fp);



0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.
Suggested Courses

783 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question