• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 210
  • Last Modified:

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

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
BCBob2001
Asked:
BCBob2001
1 Solution
 
homer99Commented:
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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now