By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,371 Members | 1,488 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,371 IT Pros & Developers. It's quick & easy.

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

P: 1

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 51112

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

program usr/bin/test

where: 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]);

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");
Nov 12 '06 #1
Share this Question
Share on Google+
1 Reply

Expert 100+
P: 1,510
run time parameters to main() are seperated by spaces, i.e. running the program
program usr/bin/test

gives is argv[1]
usr/bin/test is argv[2]

you can then use function strstr() in <string.h> to extract and 51112 from argv[1], see
Nov 13 '06 #2

Post your reply

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