Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

How to split one command line argument

Posted on 1997-07-18
2
Medium Priority
?
158 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

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

If you get a (Blue Screen of Death), your system writes a small file called a minidump. Your first step is to make certain your computer is setup to record memory dumps. Right click My Computer, choose properties. Click on the advanced tab, an…
I hope you'll find this tutorial useful and interesting. So let's try to extend Tcl with a new package.  For anyone more deeply interested please check out the book "Practical Programming in Tcl and Tk". It's really one of the best written books abo…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
Suggested Courses

609 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