parsing command line parameters

Discussion in 'C Programming' started by John Hanley, Nov 24, 2004.

  1. John Hanley

    John Hanley Guest

    What is the easiest way to parse out a number from a command line string?

    eg. PARAM=5

    I tried using strchr to get to '=' then a strtod on the remainder of the
    string.

    eg. strtod(strchr(argc[2],'='),NULL);

    It works, but I get some really weird segfault stuff on areas of my program
    that previously worked fine.

    Perhaps I am using it wrong, or perhaps I am completely off base with the
    solution.

    Also, would getopt() somehow work for this?

    I am programming in C for DOS.

    Ideas?
    Thanks a bunch!
     
    John Hanley, Nov 24, 2004
    #1
    1. Advertising

  2. In article <>,
    John Hanley <> wrote:
    >eg. strtod(strchr(argc[2],'='),NULL);


    Presumably you mean strtod(strchr(argv[2],'=')+1, NULL);

    But you really need to check that strchr() didn't return null.

    >It works, but I get some really weird segfault stuff on areas of my program
    >that previously worked fine.


    Most likely you have some other error in your program that just didn't
    happen to show up before. Try using some bounds-checking library or
    debugger to check for mistakes in your use of malloc() etc.

    -- Richard
     
    Richard Tobin, Nov 24, 2004
    #2
    1. Advertising

  3. John Hanley

    Al Bowers Guest

    John Hanley wrote:
    > What is the easiest way to parse out a number from a command line string?
    >
    > eg. PARAM=5
    >
    > I tried using strchr to get to '=' then a strtod on the remainder of the
    > string.
    >
    > eg. strtod(strchr(argc[2],'='),NULL);
    >
    > It works, but I get some really weird segfault stuff on areas of my program
    > that previously worked fine.
    >
    > Perhaps I am using it wrong, or perhaps I am completely off base with the
    > solution.
    >


    Is it possible that the string, argc[2,] does not have a '=' character
    in it?
    If the string is without a '=' then your functionstrchr will
    return NULL. This would result in the strtod call being equivalent
    to:
    strtod(NULL,NULL);
    The first argument of NULL, may be the source of your seg. fault.
    Also, even if strchr return a pointer to the '=' in the string,
    your subsequent argument in strtod would point to a string beginning
    with the '=' character, e.g. "=234".


    > Also, would getopt() somehow work for this?
    >


    I do not believe getopt is a Standard C function. So, it would not
    be a solution in the newsgroup.
    >
    > Ideas?


    There are several. Here is two.

    1}
    #include <stdlib.h>
    #include <string.h>
    char *s,
    int d;

    s = strchr(argc[2],'=');
    if(s != NULL)
    d = strtod(s+1,NULL);

    2)
    #include <stdio.h>
    int d;

    if(1 = sscanf(argc[2],"PARM=%d",&d))
    printf("d = %d\n",d);

    There are more robust solutions depending on the degree you
    want to validate the int result.


    --
    Al Bowers
    Tampa, Fl USA
    mailto: (remove the x to send email)
    http://www.geocities.com/abowers822/
     
    Al Bowers, Nov 24, 2004
    #3
  4. John Hanley

    Michael Mair Guest

    John Hanley wrote:
    > What is the easiest way to parse out a number from a command line string?
    >
    > eg. PARAM=5
    >
    > I tried using strchr to get to '=' then a strtod on the remainder of the
    > string.
    >
    > eg. strtod(strchr(argc[2],'='),NULL);


    1) Perform some error checking here to find out whether '=' was
    encountered.
    2) strrchr() is maybe a more appropriate choice
    3) strtod() skips initial _whitespace_ but not an initial '='.
    Increase the pointer returned by str[r]chr() by 1 before
    passing it.


    > It works, but I get some really weird segfault stuff on areas of my program
    > that previously worked fine.
    >
    > Perhaps I am using it wrong, or perhaps I am completely off base with the
    > solution.


    Well, this can have different reasons, among them even that
    your program worked fine but contained some bug which now
    comes to light. Without either more information about the
    rest of your program or a delivery of a good crystal ball,
    I cannot say anything but "use a debugger".


    > Also, would getopt() somehow work for this?


    I have not used it yet. As this is a POSIX function, you better
    look up your documentation or ask in another newsgroup or both.
    My manpage for getopt() seems pretty extensive.


    Cheers
    Michael
    --
    E-Mail: Mine is a gmx dot de address.
     
    Michael Mair, Nov 24, 2004
    #4
  5. "Al Bowers" <> wrote in message
    news:...
    > #include <stdlib.h>
    > #include <string.h>
    > char *s,
    > int d;
    >
    > s = strchr(argc[2],'=');
    > if(s != NULL)
    > d = strtod(s+1,NULL);


    if you use strtod(), I assume you expect a floating point value for the
    parameter.
    in this case define d as double.
    Otherwise, use strtol(s + 1, NULL, 0);

    --
    Chqrlie
     
    Charlie Gordon, Nov 24, 2004
    #5
  6. John Hanley

    Malcolm Guest

    "John Hanley" <> wrote
    > What is the easiest way to parse out a number from a command line string?
    >
    > eg. PARAM=5
    >
    > I tried using strchr to get to '=' then a strtod on the remainder of the
    > string.
    >

    Don't specify this out of choice. The reason is that a user can easily type
    PARAM = 5 or PARAM= 5 and since the shell will split command line arguments
    you get a difficult situation.

    However if you must

    if( sscanf(argv[1],"PARAM=%d", &x) == 1)
    /* process x */
    else
    /* bad argument */

    will do the trick.

    Ask Dan Pop how you match the NUL at the end of the string to make it even
    more robust.
     
    Malcolm, Nov 24, 2004
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Tal Raveh
    Replies:
    2
    Views:
    653
    Ralf Wahner
    Jun 14, 2004
  2. Jonck van der Kogel
    Replies:
    2
    Views:
    1,002
    Jonck van der Kogel
    May 27, 2004
  3. magda muskala
    Replies:
    0
    Views:
    616
    magda muskala
    Feb 15, 2005
  4. Kenneth P. Turvey
    Replies:
    1
    Views:
    392
    Paul Hamaker
    Feb 25, 2006
  5. qazmlp
    Replies:
    7
    Views:
    550
    Adrian
    Oct 31, 2004
Loading...

Share This Page