using command line arguments, which are not single chars

This is a discussion on using command line arguments, which are not single chars within the C Programming forums, part of the General Programming Boards category; Dear forum I would like to read parameters from the command line. I would like to give a parameter that ...

  1. #1
    Registered User Moses's Avatar
    Join Date
    Feb 2010
    Location
    Denmark
    Posts
    2

    using command line arguments, which are not single chars

    Dear forum

    I would like to read parameters from the command line. I would like to give a parameter that controls the overall mode of the program, valid modes are “count”, “head”, “stat” and “CSV”. For user convenience I would like to allow the use of single letter short forms “c”, “h”, “s” and the lower case form “csv”.

    I tried the code below, but it seems that argv[1] is not really a string, even though printf() seems to accept it as a string argument.

    Can any one please tell how I get the string information in argv[1] into a string variable that will allow me to something like the code below.

    I did look in the FAQ, but it only shows printf() and single char (like argv[1][1] == '-') usage.

    Best Regards
    Moses

    Code:
       if (argv[1]=="c" || argv[1]=="count") {
          printf( " mode: count\n" );
       }
       else {
          if (argv[1]=="h" || argv[1]=="head") {
             printf(" mode: head\n");
          }
          else {
             if (argv[1]=="s" || argv[1]=="stat") {
                printf(" mode: stat\n");
             }
             else {
                if (argv[1]=="csv" || argv[1]=="CSV") {
                   printf(" mode: CSV\n");
                }
                else {
                   printf(" Unknown mode: %s\n",argv[1]);
                }
             }
          }
       }

  2. #2
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    You cannot compare zero terminated strings with operator == ( that would just compare the addresss ).
    Use strcmp()

    Kurt
    Last edited by ZuK; 02-16-2010 at 03:58 AM.

  3. #3
    Registered User Moses's Avatar
    Join Date
    Feb 2010
    Location
    Denmark
    Posts
    2

    Solved ...

    Thanks ... Now it looks like below, and works perfect:

    Code:
       strcpy(cmd,argv[1]); // copy to string 'short name' variable
       for (i=0; cmd[i]; i++)
         cmd[i] = tolower(cmd[i]); // make it all lower case
    
       if (strcmp(cmd,"c")==0 || strcmp(cmd,"count")==0) {
    	   printf( " mode: Count\n" );
       }
       else {
    	   if (strcmp(cmd,"h")==0 || strcmp(cmd,"head")==0) {
    		   printf(" mode: Head\n");
    	   }
    	   else {
    		   if (strcmp(cmd,"s")==0 || strcmp(cmd,"stat")==0) {
    			   printf(" mode: Stat\n");
    		   }
    		   else {
    			   if (strcmp(cmd,"csv")==0) {
    				   printf(" mode: CSV\n");
    			   }
    			   else {
    				   printf(" Unknown mode: %s\n",cmd);
    			   }
    		   }
    	   }
       }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 10
    Last Post: 09-27-2005, 01:49 PM
  2. seg fault command line arguments
    By bazzano in forum C Programming
    Replies: 17
    Last Post: 09-14-2005, 08:31 PM
  3. Command Line Arguments Dilemma!!
    By Want2Know in forum C Programming
    Replies: 2
    Last Post: 09-14-2005, 11:35 AM
  4. Counting how many chars in a string
    By tigs in forum C Programming
    Replies: 4
    Last Post: 08-05-2002, 01:25 AM
  5. fancy strcpy
    By heat511 in forum C++ Programming
    Replies: 34
    Last Post: 05-01-2002, 05:29 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21