473,386 Members | 1,908 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

c: Parse input command for main(int argc, char *argv[])

Hello,

I am new to c and need help with a current program I am working on.

Currently, the program (code below) input is as follows:

program apple.test.com 51112

What I need is (essentially three arguments with a ':' space between the hostname and port number, and a third argument):

program apple.test.com:51112 usr/bin/test

where:

apple.test.com is argv[1]
51112 is argv[2]
usr/bing/test is argv[3]

Thanks guys.

Here is a bit of the current client file which needs to be modified.
________________________________________

int main(int argc, char *argv[])
{
int sockfd, portno, n;
struct sockaddr_in serv_addr;
struct hostent *server;

char buffer[256];


// get proper number of input arguments before proceeding
if (argc != 3) {
fprintf(stderr,"usage %s <hostname> <port>\n", argv[0]);
exit(0);
}

portno = atoi(argv[2]);
sockfd = socket(AF_INET, SOCK_STREAM, 0);

if (sockfd < 0)
error("ERROR opening socket");


server = gethostbyname(argv[1]);
if (server == NULL) {
fprintf(stderr,"ERROR, no such host\n");
exit(0);
}
Nov 12 '06 #1
1 10951
horace1
1,510 Expert 1GB
run time parameters to main() are seperated by spaces, i.e. running the program
program apple.test.com:51112 usr/bin/test

gives

apple.test.com:51112 is argv[1]
usr/bin/test is argv[2]

you can then use function strstr() in <string.h> to extract apple.test.com and 51112 from argv[1], see
http://www.cplusplus.com/ref/cstring/strstr.html
Nov 13 '06 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

19
by: Steven T. Hatton | last post by:
The short sample program listed below has some features that I find to be bad style. In particular, they fail to communicate the connection between names used in this program and the location in...
14
by: Hal Styli | last post by:
Is this a style thing? int main(int argc, char *argv ) or int main(int argc, char **argv ) i.e. *argv or **argv Why choose the latter?
13
by: Sokar | last post by:
I have my main function set up as int main(int argv, char *argv) so taht i can read in a variable which is passed to the program on the command line. The problem is that main calls other...
7
by: =?Utf-8?B?Vmlu?= | last post by:
Hi, I have a question. I created a simple executable program using Visual C++ from Visual Studio 6.0 This program is called from a script that passes in one argument. Now, my question is: ...
7
by: william | last post by:
I wonder how 'main(int argc, char ** argv)' is implemented? How does it get the string literals separated by whitespace from the stdin stream? And is there any difference between 'char**' and...
3
by: Bill Cunningham | last post by:
I have been having a little trouble with this page. http://www.physics.drexel.edu/courses/Comp_Phys/General/C_basics/#command-line I wanted to create a command called div that takes two...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.