Solved

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

Posted on 2000-04-11
1
199 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 2

Accepted Solution

by:
homer99 earned 50 total points
ID: 2706109
for reading in words from a file you would not use scanf.  scanf is to read in from input stream.

if this is part of your list of words:

char string1[] = "string1";
char string2[] = "string2";
..
..
..

in main program we execute:

main()
{
  FILE *fptr_read;
  char word[MAX_REC_SIZE + 1 ];
  fptr_read = fopen("FILENAME", "r" );
  while( fgets( word, MAX_REC_SIZE + 1, fptr_read ) != NULL )
  {
    if(strcmp(string1,word) == 0 )
      counter++;
  }
..
..
..
this is not the complete solution because it sounds like homework, but this should give you a good idea of what to do.
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
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…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.

911 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

22 Experts available now in Live!

Get 1:1 Help Now