Solved

How to split one command line argument

Posted on 1997-07-18
2
149 Views
Last Modified: 2013-12-25
I have a C program that takes a command line argument like this:
myprogram 5.100.3.6

or called from a web browser:
myprogram?5.100.3.6

I need to be able to separate the four numbers and put them in separate variables, like this:

p should have the value 5
a should have the value 100
w should have the value 3
pg should have the value 6

Hou do I do it? I can go so far as to have a variable that holds the entire thing "5.100.3.6" using the form main(int argc, char **argv), but I can not figure out how to split it. If I would use PERL I would do it like this:

@argument = split(/\./, $ARGV[0]);

but in C .... I don't know. Please help.

Thank you.
0
Comment
Question by:neis
2 Comments
 
LVL 5

Accepted Solution

by:
julio011597 earned 50 total points
ID: 1829157
This should work (man sscanf(3)):

--//--
int p, a, w, pg;

sscanf(argv[1], "%d.%d.%d.%d", &p, &a, &w, &pg);
--//--

(note that in C argv[0] contains the commund run)

Rgds, julio
0
 
LVL 1

Author Comment

by:neis
ID: 1829158
Thank you.

For other people...there is another answer to this question at
http://www.experts-exchange.com/Q.8630010596

Both of them work.
0

Featured Post

The New “Normal” in Modern Enterprise Operations

DevOps for the modern enterprise offers many benefits — increased agility, productivity, and more, but digital transformation isn’t easy, especially if you’re not addressing the right issues. Register for the webinar to dive into the “new normal” for enterprise modern ops.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Need to Modify a Script I found 5 108
User profile Size Report 3 84
Where do I learn ES6? 3 78
IDE for Python 5 25
This tutorial will discuss fancy secure registration forms, with AJAX technology support. In this article I assume you already know HTML and some JS. I will write the code using WhizBase Server Pages, so you need to know some basics in WBSP (you mig…
Batch, VBS, and scripts in general are incredibly useful for repetitive tasks.  Some tasks can take a while to complete and it can be annoying to check back only to discover that your script finished 5 minutes ago.  Some scripts may complete nearly …
Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question