#1
| |||
| |||
Arguments in C I have management issues of args/params via command line. I have a program that accepts 4 arguments to max, but only one is mandatory, others are optional. Each argument to a "value" ie: Code: // minimum : ./myProg -a 2 // maximum : ./myProg -a 2 -b 4 -c 3 -d 5 Code: if( strcmp(argv[1],"-a" )==0 ) Code: if( int nb = atoi(argv[2]) ); Is there a way clean to handle arguments? switch/case? if/else? other? |
#2
| |||
| |||
Re: Arguments in C Quote:
Code: #include <stdio.h> #include <stdlib.h> #include <getopt.h> int main(int nb, char * argv[]) { int ch; while ((ch = getopt(nb, argv, "a:b:c:d:" )) > 0) { switch (ch) { case 'a': case 'b': case 'c': case 'd': fprintf(stderr, "option '%c', value = %s\n", ch, optarg); break; case ':': fprintf(stderr, "option '%c', missing value\n", optopt); break; case '?': fprintf(stderr, "invalid switch -%c\n", optopt); } } while (optind < nb) { fprintf(stderr, "remaining option %s\n", argv[optind]); optind ++; } } |
#3
| |||
| |||
Re: Arguments in C You can browse each table that has a corresponding argument to verify the internal things, something like as below: Code: int is_number(char *str) { int i; int boolean; i = 0; boolean = 1; while (str[i] != '\0' && boolean == 1) { boolean = 0; if (str[i] >= '0' && str[i] <= '9') boolean = 1; i++; } return (boolean); } |
#4
| |||
| |||
Re: Arguments in C Thank you very much. Another question I'd like to know how to retrieve arguments through the pipe like this: Code: $ cat data.txt t=1 s=2 e=3 $ cat data.txt | myprog.exe $ // Does not work |
![]() |
|
Tags: arguments, c language, parameters |
Thread Tools | Search this Thread |
|
![]() | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Arguments passed by value and reference | Chellam | Software Development | 4 | 30-12-2010 04:57 AM |
How to get Hyper-V events arguments value? | jhon | Operating Systems | 5 | 06-04-2010 12:48 AM |
String in the method arguments | DANIEL 602 | Software Development | 3 | 29-12-2009 02:21 PM |
Passing arguments to a function | Aman 1 | Software Development | 3 | 09-12-2009 01:11 PM |
PHP command line arguments | Sori | Software Development | 3 | 24-09-2009 12:45 PM |