?
Solved

How to split one command line argument

Posted on 1997-07-18
2
Medium Priority
?
154 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 5

Accepted Solution

by:
julio011597 earned 200 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

Give Your Engineering Team a Productivity Boost

Learn why container technology is so powerful and how it can provide your team with productivity gains and other benefits.

Question has a verified solution.

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

It is a general practice to get rid of old user profiles on a computer  in a LAN environment. As I have been working with a company in a LAN environment where users move from one place to some other place at times. This will make many user profil…
This article will show, step by step, how to integrate R code into a R Sweave document
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 …
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.
Suggested Courses

764 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