Solved

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

Posted on 2000-04-11
1
187 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 20 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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
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.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.

813 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now