Thread: number of occerrence of words

  1. #1
    Registered User
    Join Date
    Jul 2007
    Posts
    24

    Question number of occerrence of words

    Hi,
    I'm trying to write a program for the number of occerrence of a word from a file,
    so I need to open a user input file as argv[1] and get the search word from argv[2] and print out the number of occerrence of that word.
    can someone help me please.

    Code:
    #include <stdio.h>
    #include <ctype.h>
    #include <stdafx.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <conio.h>
    #include <string.h>
    
    
    
    int process_file( FILE *fp );
    
    
    int _tmain(int argc, _TCHAR* argv[])
    {
      if ( argc > 1 ) {
        FILE *fp = fopen( argv[1], "r" );
    
        if ( fp ) {
          if ( !process_file( fp ) ) {
            // failure means a stream error or bad file
            perror( "error reading from the file" );
          }
    
          fclose( fp );
        } else {
          perror( "error opening the file" );
        }
      } else {
        fprintf( stderr, "usage: prog <filename>\n" );
      }
    
      return 0;
    }
    
    
    int process_file( FILE *fp ) {
    	char buff[1000]={0},
    		 tempStr[300]={0},
    		 *tokenPtr;
           
        int n = 0;
      while ( fgets( tempStr, sizeof tempStr, fp ) != NULL ) {
    	  strcat( buff, tempStr );
      }
      tokenPtr = strtok( buff, ". !, \n" );
    
      do {
    	  n=strcmp (tokenPtr,argv[2]);
    	  n++;
      }while ( tokenPtr != NULL );
      tokenPtr = strtok( NULL, " ! -.,\n" );
      printf( "%8s%15s\n", "searched Word", "Occurrences" );
      printf("%4c%4d",argv[2],n);
    }

  2. #2
    Cogito Ergo Sum
    Join Date
    Mar 2007
    Location
    Sydney, Australia
    Posts
    463
    how long have you been programming for? because that sure looks very complicated!!!

  3. #3
    Registered User
    Join Date
    Jul 2007
    Posts
    24
    not very long,
    but I'm really desperate for this program,
    I have to get it working soon, It's my university project.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    When you call process_file(), you probably want to pass "argv[2]" to that function, rather than using it as if it was a global. This function doesn't have a "argv" parameter, so it's an unknown variable in that function.

    --
    Mats

  5. #5
    Registered User
    Join Date
    Jul 2007
    Posts
    24
    I tried the following but it still doesn't work.
    Code:
    int process_file( FILE *fp ) {
    	char buff[1000]={0},
    		 tempStr[300]={0},
    		 string[100]={argv[2]},
    		 *tokenPtr;
    	       
        int n = 0;
      while ( fgets( tempStr, sizeof tempStr, fp ) != NULL ) {
    	  strcat( buff, tempStr );
      }
      tokenPtr = strtok( buff, ". !, \n" );
       do {
    	  n=strcmp (tokenPtr,string);
    	  n++;
      }while ( tokenPtr != NULL );
      tokenPtr = strtok( NULL, " ! -.,\n" );
      printf( "&#37;8s%15s\n", "searched Word", "Occurrences" );
      printf("%4c%4d",string,n);
    }

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Also, use a const char array for your "strtok" second parameter (so that you don't copy the same string literal twice, potentially creating bugs by having them different), and add to it: ":;?\t". (You may need to add more if the text you are parsing is something like program code, such as "()[]/+-%&" etc. Quotes would also count as word separators, I would think.



    --
    Mats

  7. #7
    Registered User
    Join Date
    Jul 2007
    Posts
    24
    when I try to run the code above it still gives me the following error:

    error C2065: 'argv' : undeclared identifier

    what should I do to fix that.

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You aren't passing argv[2] to process_file, you are just trying to USE that in the function.

    --
    Mats

  9. #9
    Registered User
    Join Date
    Jul 2007
    Posts
    24
    sorry for being slow but how can I pass argv[2] to process_file.

  10. #10
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Something like "process_file(fp, argv[2])" perhaps?

    --
    Mats

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Issue w/ Guess My Number Program
    By mkylman in forum C++ Programming
    Replies: 5
    Last Post: 08-23-2007, 01:31 AM
  2. Problem with malloc() and sorting words from text file
    By goron350 in forum C Programming
    Replies: 11
    Last Post: 11-30-2004, 10:01 AM
  3. Number to words
    By Hexxx in forum C Programming
    Replies: 5
    Last Post: 12-17-2003, 03:01 AM
  4. Program to show the number of occurrences of words...
    By Nutshell in forum C Programming
    Replies: 5
    Last Post: 01-26-2002, 06:44 PM
  5. Random Number problem in number guessing game...
    By -leech- in forum Windows Programming
    Replies: 8
    Last Post: 01-15-2002, 05:00 PM