Thread: need help with command line arguments

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

    Question need help with command line arguments

    Hi everyone.
    I have to do the following for my project,
    You are required to develop a program to search a test file for the number of occurrences of at least two words. Output from the program should be produced both on the display and held in second text file.

    The input file, output file and the search words should be specified as command line arguments for the program.
    i.e mysearchprog inputfile outputfile the but

    where mysearchprog is the name of the program, inputfile and outputfile are the names of the input and output files and the and but are the search words.

    Hint :
    Your application should contain a number of functions.
    Remember that words in the text file can be terminated with spaces, commas, full stops, etc.

    I don't quite understand the command line arguments and how they're excecuted.
    I've written the code below,it opens a file and gives the number of occurrence of 1-letter word , 2-letter word and so one,
    how can I use the command line arguments and how can I search for specific words like the, but.
    can someone one help me please.

    Code:
    #include "stdafx.h"
    #include <stdio.h>
    #define LEN_TOTAL 21
    
    int _tmain(int argc, _TCHAR* argv[])
    {
    	 int word_length_occurrence[ LEN_TOTAL ] = { 0 };
    	 char string[700]={0},
    	      tempStr[300]={0},
    		  *tokenPtr;
    	   size_t i,
              maxLength = 0;
    	
            
    	 FILE * pFile;
                pFile = fopen ("input.txt","r");
         if (pFile==NULL) perror ("Error opening file");
      else
      {
         
    	 while (fgets( tempStr, sizeof( tempStr ), pFile ) != NULL)
    {
    	  strcat( string, tempStr );
    }
    		   tokenPtr = strtok( string, " \n" );
    	
    	  while ( tokenPtr != NULL ) {
          maxLength = strlen( tokenPtr );
          word_length_occurrence[ maxLength ]++;
          tokenPtr = strtok( NULL, " ! -.,\n" );
    	 }
          printf( "%8s%15s\n", "Word Length", "Occurrences" );
    
    	  for ( i = 1; i < LEN_TOTAL; i++ ) 
          printf( "%4d%15d%c", i, word_length_occurrence[ i ], '\n' );
    	  
      
    	printf( "\n" );
        getchar();
    	fclose (pFile);
    }
       
    FILE * pFile2;
    		pFile2 = fopen ("output.txt","w+");
    		if (pFile2 != NULL) {
    		fprintf( pFile2,"%8s%15s\n", "Word Length", "Occurrences" );
    
    		for ( i = 1; i < LEN_TOTAL; i++ ) {
          fprintf( pFile2,"%4d%15d%c", i, word_length_occurrence[ i ],'\n' );
    	  
    		fclose(pFile2);
    		}
    		}
      
    
      
      
      return 0;
    }

  2. #2
    Fear the Reaper...
    Join Date
    Aug 2005
    Location
    Toronto, Ontario, Canada
    Posts
    625
    First off, learn to indent your code properly (and use spaces instead of tabs).

    But your question is simple.

    Code:
    int main(int argc, char*[] argv)
    Assuming that's what your main function looks like, argc will hold the number of command line arguments, argv will be an array of those same command line arguments.
    Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" It's great to see the educational system moving in the right direction

  3. #3
    Registered User
    Join Date
    Jul 2007
    Posts
    24
    what about searching for the specific words as command line arguments such as the , but.

  4. #4
    Fear the Reaper...
    Join Date
    Aug 2005
    Location
    Toronto, Ontario, Canada
    Posts
    625
    example :

    ./myprog file1.txt file2.txt word1 word2

    argc = 5
    argv[0] = myprog
    argv[1] = file1.txt
    argv[2] = file2.txt
    argv[3] = word1
    argv[4] = word2
    Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" It's great to see the educational system moving in the right direction

  5. #5
    Registered User
    Join Date
    Aug 2007
    Posts
    4
    The C standard library contains a number of string utility functions (I see you've already used a few of them). Look up strstr() and strcmp().

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    . . . and include <string.h>, whatever you do, since you're already using some functions from it.

    Code:
    int main(int argc, char*[] argv)
    Actually, that's not right. My compiler doesn't like it. You should put the [] after argv.
    Code:
    int main(int argc, char *argv[])
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  7. #7
    Registered User
    Join Date
    Jul 2007
    Posts
    24
    how can I run the program with command line arguments, do I just run it by pressing F5 or is there another way,
    cos pressing f5 doesn't work

  8. #8
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Run it through the terminal/command line,

    ie On Windows, I think it's something like: Start -> Run -> cmd (Or command.com if it's 9x based)
    Then locate your compiled binary, ie with 'cd', then run it as such
    Code:
    blah.exe arg1 arg2 arg3

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I personally use command-line prompt to do this type of stuff, but if you want, there is a setting for "command line" under "project->properties" (make sure you click on the executable "product", rather than some individual .c or .cpp file).

    Just remember what you set it to last time, because I've more than once "confused myself" thinking that the command line is "x", when it's actually "y", and wondering why the application isn't doing what I wanted.

    --
    Mats

  10. #10
    Registered User
    Join Date
    Jul 2007
    Posts
    24
    I don't see any exe files in the directory.
    and when I use cpp file it says it not recognised as internal or external command

  11. #11
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You are using visual studio, yes? The .exe file is in a directory called "Debug" or "Release" (depending on which build-mode you are using - and it CAN be changed, but I wouldn't recommend you mess with those things, as it can get very confusing to yourself and others).

    Also, REMEMBER that when you switch from using the debug version to the release version (or other way around), you will need to move to the other directory, or you won't see the difference when you compile the code again with some changes... (Guess how I learned that one :-) )

    --
    Mats

  12. #12
    Registered User
    Join Date
    Jul 2007
    Posts
    24
    thanks I found the exe file
    but it still tells me that it's not recognised as internal or external command
    when I try to run it,
    I use start > run > cmd
    then bring it to the debug directory where the exe file is and then I type
    exe command line xxx yyy zzz

    exe command line is the name of my exe file.

  13. #13
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by cyber_tech View Post
    thanks I found the exe file
    but it still tells me that it's not recognised as internal or external command
    when I try to run it,
    I use start > run > cmd
    then bring it to the debug directory where the exe file is and then I type
    exe command line xxx yyy zzz

    exe command line is the name of my exe file.
    Can you copy and paste the exact command line from your command prompt? [Mark with the mouse and hit enter to copy from a command window]

    --
    Mats

  14. #14
    Registered User
    Join Date
    Jul 2007
    Posts
    24
    its working now
    thanks for the help Mats

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Beginner Needs help in Dev-C++
    By Korrupt Lawz in forum C++ Programming
    Replies: 20
    Last Post: 09-28-2010, 01:17 AM
  2. GradeInfo
    By kirksson in forum C Programming
    Replies: 23
    Last Post: 07-16-2008, 03:27 PM
  3. command line arguments
    By vurentjie in forum C Programming
    Replies: 3
    Last Post: 06-22-2008, 06:46 AM
  4. NULL arguments in a shell program
    By gregulator in forum C Programming
    Replies: 4
    Last Post: 04-15-2004, 10:48 AM
  5. registry, services & command line arguments.. ?
    By BrianK in forum Windows Programming
    Replies: 3
    Last Post: 03-04-2003, 02:11 PM