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
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