parsing command line parameters

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • John Hanley

    #1

    parsing command line parameters

    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(a rgc[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!



  • Richard Tobin

    #2
    Re: parsing command line parameters

    In article <1101310811.483 559@proxy2.srv. ualberta.ca>,
    John Hanley <jdhanley@telus planet.net> wrote:[color=blue]
    >eg. strtod(strchr(a rgc[2],'='),NULL);[/color]

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

    But you really need to check that strchr() didn't return null.
    [color=blue]
    >It works, but I get some really weird segfault stuff on areas of my program
    >that previously worked fine.[/color]

    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

    Comment

    • Al Bowers

      #3
      Re: parsing command line parameters



      John Hanley wrote:[color=blue]
      > 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(a rgc[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.
      >[/color]

      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,NUL L);
      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".

      [color=blue]
      > Also, would getopt() somehow work for this?
      >[/color]

      I do not believe getopt is a Standard C function. So, it would not
      be a solution in the newsgroup.[color=blue]
      >
      > Ideas?[/color]

      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: xabowers@myrapi dsys.com (remove the x to send email)
      Latest news coverage, email, free stock quotes, live scores and video are just the beginning. Discover more every day at Yahoo!


      Comment

      • Michael Mair

        #4
        Re: parsing command line parameters



        John Hanley wrote:[color=blue]
        > 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(a rgc[2],'='),NULL);[/color]

        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.

        [color=blue]
        > 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.[/color]

        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".

        [color=blue]
        > Also, would getopt() somehow work for this?[/color]

        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.

        Comment

        • Charlie Gordon

          #5
          Re: parsing command line parameters

          "Al Bowers" <xabowers@rapid sys.com> wrote in message
          news:30jrk9F306 oopU1@uni-berlin.de...[color=blue]
          > #include <stdlib.h>
          > #include <string.h>
          > char *s,
          > int d;
          >
          > s = strchr(argc[2],'=');
          > if(s != NULL)
          > d = strtod(s+1,NULL );[/color]

          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



          Comment

          • Malcolm

            #6
            Re: parsing command line parameters


            "John Hanley" <jdhanley@telus planet.net> wrote[color=blue]
            > 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.
            >[/color]
            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.


            Comment

            Working...