Results 1 to 4 of 4

Thread: Arguments in C

  1. #1
    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?

  2. #2
    Join Date
    May 2008
    Posts
    271

    Re: Arguments in C

    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 ++;
        }
    }

  3. #3
    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.

  4. #4
    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?

Similar Threads

  1. Arguments passed by value and reference
    By Chellam in forum Software Development
    Replies: 4
    Last Post: 30-12-2010, 04:57 AM
  2. How to get Hyper-V events arguments value?
    By jhon in forum Operating Systems
    Replies: 5
    Last Post: 06-04-2010, 12:48 AM
  3. String in the method arguments
    By DANIEL 602 in forum Software Development
    Replies: 3
    Last Post: 29-12-2009, 02:21 PM
  4. Passing arguments to a function
    By Aman 1 in forum Software Development
    Replies: 3
    Last Post: 09-12-2009, 01:11 PM
  5. PHP command line arguments
    By Sori in forum Software Development
    Replies: 3
    Last Post: 24-09-2009, 12:45 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,751,520,691.01958 seconds with 16 queries