Solved

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

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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

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…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
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 reading files in the C programming language.

760 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

21 Experts available now in Live!

Get 1:1 Help Now