Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read

Reply
 
Thread Tools Search this Thread
  #1  
Old 24-10-2009
Member
 
Join Date: Jun 2009
Posts: 3,960
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
To get my "-a" its argv[1], to recover the "2" its argv[2] etc. To check if I have my "a", I have

Code:
if( strcmp(argv[1],"-a" )==0 )
Then, if the check for user to enter a good number (2 here) just after the "a", what should I do? Because if I am having:

Code:
if( int nb = atoi(argv[2]) );
Except that if argv [2] (the "2" here) has not been returned by the user, it throws me a segfault because argv[2] does not exist. How can we then test for the existence (or not) of an argument? (A kind of isset () as php ..)

Is there a way clean to handle arguments? switch/case? if/else? other?
Reply With Quote
  #2  
Old 24-10-2009
XSI XSI is offline
Member
 
Join Date: May 2008
Posts: 271
Re: Arguments in C

Quote:
switch/case? if/else? other?
Yes, getopt and forget everything (memory, not tested):

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 ++;
    }
}
Reply With Quote
  #3  
Old 24-10-2009
Member
 
Join Date: Nov 2005
Posts: 1,323
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);
}
The above code returns 1 if the string contains only digits, and 0 if it contains something else.
Reply With Quote
  #4  
Old 24-10-2009
Member
 
Join Date: Jun 2009
Posts: 3,960
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
I would like therefore that the arguments contained in data.txt come to setting my program. Is there a way to do it?
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "Arguments in C"
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


All times are GMT +5.5. The time now is 01:15 AM.