473,434 Members | 1,521 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,434 software developers and data experts.

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

Nov 14 '05 #1
5 2349
In article <11***************@proxy2.srv.ualberta.ca>,
John Hanley <jd******@telusplanet.net> 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
Nov 14 '05 #2


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: xa******@myrapidsys.com (remove the x to send email)
http://www.geocities.com/abowers822/

Nov 14 '05 #3


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.

Nov 14 '05 #4
"Al Bowers" <xa******@rapidsys.com> wrote in message
news:30*************@uni-berlin.de...
#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

Nov 14 '05 #5

"John Hanley" <jd******@telusplanet.net> 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.
Nov 14 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Fraser Gordon | last post by:
Hello, Hopefully someone can help me out with this issue... I have a python script that needs to run a shell command and be able to react to output from that command as it occurs (as opposed...
2
by: Naina | last post by:
Hi , I am trying to read parameters from the command line. I enter -instrType"S2234".I need to extract s2234 and store in Say $test. I am able to separate -instrType as shown below ,but I am...
1
by: mriedel | last post by:
I'm using the InstallContext class to parse the command-line arguments of a console application. The arguments are in the form of "-file=myFile.txt -flag", and the InstallContext object gives me what...
6
by: John Paulsson | last post by:
Is there a C# pattern or perhaps a .NET framework way of parsing a command lines, supporting quoted filepaths etc? (I'm not talking about the current applications command arguments. I've got...
2
by: siliconpiNOSPAM | last post by:
Hi, I'm writing a program that should accept the following parameters: XMLfile (required) Logfile (required) /A (append flag, optional) /D 123 (delay value, optional, but # should follow /D...
5
by: clsmith66 | last post by:
I've been asked to find out if a project is possible, but I'm not having much luck finding the information I need, I hope some one can help. I need to see if I can build a windows service on the...
4
by: Jim Langston | last post by:
In my program I am accepting messages over the network and parsing them. I find that the function that does this has gotten quite big, and so want to break the if else code into functions. I...
2
by: explode | last post by:
I made nova oledbdataadapter select update insert and delete command and connection veza. dataset is Studenti1data, I made it by the new data source wizard,and made datagridview and bindingsource...
13
by: Chris Carlen | last post by:
Hi: Having completed enough serial driver code for a TMS320F2812 microcontroller to talk to a terminal, I am now trying different approaches to command interpretation. I have a very simple...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.