Solved

How to split one command line argument

Posted on 1997-07-18
2
146 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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Introduction This tutorial will give you a fast look what you can do with WhizBase. I expect you already know how to work with HTML at least, and that you understand the basics of the internet and how the internet works. WhizBase is a server-s…
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…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
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)

708 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now