switch statement / command line

This is a discussion on switch statement / command line within the C Programming forums, part of the General Programming Boards category; Hi, I in desperate need of help. I have most of my functions in a program working, but in order ...

  1. #1
    Unregistered
    Guest

    Exclamation switch statement / command line

    Hi, I in desperate need of help. I have most of my functions in a program working, but in order to call them I need to use the command line arguments

    In order to access a particular function like the create mode the user woul input

    dataprog C filename.dat

    so I try inputting argv[1] in the switch, but everything I try doesn't work

    here is the code for main

    int main(int argc, char *argv[])
    {

    if(argc !=3)
    {
    printf("Usage: dataprog <Mode C A or Q> <filename>\n");
    exit(1);
    }

    switch (argv[1]) {
    case 'A' : append_mode(char argv[]);
    break;
    case 'C' : create_mode(char argv[]);
    break;
    case 'Q' : query_mode(char argv[]);
    break;
    default: printf("Illegal code, enter (C)reate, (A)ppend or (Q)uery.\n");
    }

    return 0;
    }

    Your help would be very much appreciated

  2. #2
    Unregistered
    Guest
    argv[1] is a pointer to the start of the second command line string... you just need to look at the value of the first character in the string, correct? Try de-referencing the pointer (*argv[1])...?

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    The reason it doesn't work, is because 'argv[1]' is a string.
    Try 'argv[1][0]', or '*argv[1]'.
    Code:
    switch (*argv[1]) { 
        case 'A' : append_mode(char argv[]); break; 
        case 'C' : create_mode(char argv[]); break; 
        case 'Q' : query_mode(char argv[]); break; 
        default: printf("Illegal code, enter (C)reate, (A)ppend or (Q)uery.\n"); 
    }
    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    Blank
    Join Date
    Aug 2001
    Posts
    1,034
    Try something like this

    Code:
    #include <stdio.h>
    
    void create_file(char* fn);
    
    int main(int argc, char* argv[])
    {
         int i;
         for (i = 1; i < argc; ++i) {
                 if (!strcmp(argv[i], "-c")) {
                        if (argv[++i]) 
                               create_file(argv[i]);
                        else
                               fprintf(stderr, "-c must have argument\n");
                  }
    
                  /* more code */
         }
    
         return 0;
    }

  5. #5
    Registered User
    Join Date
    Dec 2001
    Posts
    2

    use the value in the address location

    Hi

    Actually the argv[1] will be the starting address of the argv[1] string.

    if you give argv[1] in the switch case that means you are checking with the address of argv[1], which would not satisfy your check, our purpose would be to check withthe content...

    Please change switch as

    switch(*argv[1])
    {
    }

    It will work properly


    All the best

  6. #6
    Registered User
    Join Date
    Dec 2001
    Posts
    421
    you should use GetOpt() for stuff like this.. it makes parsing the command line really easy.
    Quidquid latine dictum sit, altum sonatur.
    Whatever is said in Latin sounds profound.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Mutli Switch statement help
    By elwad in forum C Programming
    Replies: 9
    Last Post: 05-09-2009, 03:19 AM
  2. Switch statement / default:
    By kcpilot in forum C Programming
    Replies: 4
    Last Post: 12-02-2008, 02:14 PM
  3. switch statement
    By guillermoh in forum C Programming
    Replies: 5
    Last Post: 03-10-2008, 02:17 PM
  4. switch statement issues...(undeclared identifiers)
    By mero24 in forum C++ Programming
    Replies: 2
    Last Post: 02-19-2005, 07:05 PM
  5. Efficiency with the switch() statement...
    By Unregistered in forum C++ Programming
    Replies: 3
    Last Post: 10-14-2001, 02:47 PM

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