parsing command line parameters

J

John Hanley

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!
 
R

Richard Tobin

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
 
A

Al Bowers

John said:
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.

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

Michael Mair

John said:
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
 
C

Charlie Gordon

Al Bowers said:
#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);
 
M

Malcolm

John Hanley said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,012
Latest member
RoxanneDzm

Latest Threads

Top