im looking for a good way of using command line arguements in a program. like app -x "some vaule". how do you guys do it? right now, im in if hell tying to get this right. is any other way?
thanks
im looking for a good way of using command line arguements in a program. like app -x "some vaule". how do you guys do it? right now, im in if hell tying to get this right. is any other way?
thanks
argv[0] is a null-terminated string of the name of your program (though what it actually contains can vary)Code:int main (int argc, char *argv[])
argv[1] is a null-terminated string of the first argument the user passed to your program
argv[2] is the second arg
argv[argc-1] is the last arg that was passed
hello, internet!
..... and finally (to continue moi's help).... argv[argc] is a null pointer to provide a redundant check for the end of the list.
When all else fails, read the instructions.
If you're posting code, use code tags: [code] /* insert code here */ [/code]
Have you looked at getopt() ?
something like:
Code:#include <unistd.h> int main(int argc, char **argv) { int opt; while((opt = getopt(argc, argv, "p:V")) != -1) { switch(opt) { case 'p': printf("argument p = %s\n", optarg); break; case 'V': verbose++; break; case ':' printf("Option %c needs a value\n", opt); break; case '?': printf("invalid option?\n"); break; } } }
getopt(3)
Last edited by jabrams; 08-03-2002 at 04:29 PM.
Code:#include <stdio.h> void DefaultMessage ( void ); int main ( int argc, char * argv[] ) { if (argc == 2) { if(argv[1][0] == '?') { printf("Help stuff would go here.\n"); } else { printf("%s \n", argv[1]); } } else if ( argc >= 3 ) { printf("Invalid input:\n"); printf("Only one command-line parameter allowed.\n"); } else { DefaultMessage(); } return 0; } void DefaultMessage ( void ) { printf("CmdLine.exe:\n"); printf("The purpose of this program is to demonstrate command line arguments.\n"); printf("This program will print whatever you type on the screen.\n\n"); printf("Example:\n"); printf("C:\\>CmdLine.exe Hmmm\n"); printf("Hmmm\n"); }
The world is waiting. I must leave you now.