Solved

How to split one command line argument

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Ever wondered how to display how many visitors you have online. In this tutorial I will show you an easy but effective way to display the number of online visitors in WhizBase. In this article I assume you have read my previous articles and know …
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…
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

920 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