Thread: command line

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    3

    command line

    Hi! This is my first post in this forum. I need to write a command-line program with a single parameter, the name of a text file, and output the number of paragraphs in it. I already did the function to do this and it is working oki. If no parameter is given, or the parameter is '-h', '?' or '--help', output usage information. i already did this.. but i do not know how to continue.


    Code:
    #include <stdio.h>
    #include <string.h>
    
    int counter (char filename[30]);
    FILE *fileptr;
    
    int main(int argc, char *argv[])
    {
    	char filename [30];
    	
    	if (argv > 1)
       {
       	strcpy (filename,argv[1]); 
       	
    		printf ("&#37;d",counter(argv[1])); // counter (  ) is the function to count the paragraphs
       }
       else
       {
       	printf ("help");
       }
       getchar();
    	getchar();
       return 0;
    }
    thanks in advance

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by mariacolette View Post
    Hi! This is my first post in this forum. I need to write a command-line program with a single parameter, the name of a text file, and output the number of paragraphs in it. I already did the function to do this and it is working oki. If no parameter is given, or the parameter is '-h', '?' or '--help', output usage information. i already did this.. but i do not know how to continue.
    Well, if you've already done all the parts, what's left to do? My Hat of Guessing thinks you want to know how to check against the parameters listed above, but isn't very sure. (The answer: strcmp.)

    Quote Originally Posted by mariacolette View Post
    Code:
    #include <stdio.h>
    #include <string.h>
    
    int counter (char filename[30]);
    FILE *fileptr;
    
    int main(int argc, char *argv[])
    {
    	char filename [30];
    	
    	if (argv > 1) // this line should throw many errors
       {
       	strcpy (filename,argv[1]); 
       	
    		printf ("%d",counter(argv[1])); // counter (  ) is the function to count the paragraphs
       }
       else
       {
       	printf ("help");
       }
       getchar();
    	getchar();
       return 0;
    }
    Is the bit in red a typo because you didn't want to copy-paste or something more sinister?

  3. #3
    Registered User
    Join Date
    Jan 2008
    Posts
    3
    i do not know how to use the parameters.. argv and argc. and how to check the inputs.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    argc is the number of "things" on the command line; that is, it is the number of elements in the argv array. The argv is an array of "strings" (char *) that your program gets from the command line: argv[0] is the name of the program, argv[1] is the first command-line parameter, etc. To compare strings, you need strcmp as mentioned above.

  5. #5
    Registered User
    Join Date
    Jan 2008
    Posts
    3
    10qs for your help... i solved the problem

Popular pages Recent additions subscribe to a feed