To fetch data through command line arguments

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Mar 2007
Posts: 19
Reputation: Masood Ali is an unknown quantity at this point 
Solved Threads: 1
Masood Ali's Avatar
Masood Ali Masood Ali is offline Offline
Newbie Poster

To fetch data through command line arguments

 
0
  #1
Mar 15th, 2007
Hi All.
I m a C++ programmer and I m trying to understand a piece of code.
It fetches the values through command line arguments and makes a decision on the basis of these values. This code is working well but I dont know how?

  1.  
  2. for (int i = 0; i < argc; i++)
  3.  
  4. if (strnicmp(argv[i],"/id",3) == 0)
  5. {
  6. strncpy(ID,argv[i] + 3,40); //ID is defined as a char array of size 40
  7. }

kindly tell me how does it work...?
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,500
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1478
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: To fetch data through command line arguments

 
0
  #2
Mar 15th, 2007
argc is the number of strings in the argv array. argv[0] is always the name of the executable program and will never ever contain a command-line argument.

>>argv[i] + 3
all that does is bypass the first 3 characters of the ith string.

The algorithm you posted doesn't work at all if you put a space between "/id" and whatever is supposed to follow. For example, if you typed this:
  1. c:\myprogram /id something <Enter key>
In the above the program will get two command-line arguments, not one. argv[1] will be "/cd" and argv[2] will be "something". And that will break the algorithm you posted.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 19
Reputation: Masood Ali is an unknown quantity at this point 
Solved Threads: 1
Masood Ali's Avatar
Masood Ali Masood Ali is offline Offline
Newbie Poster

Re: To fetch data through command line arguments

 
0
  #3
Mar 15th, 2007
Thanks for the help.
Would You please tell me that why it will not work for space, if i give arguments as
  1. c:\myprogram /idTheAdminId /pwTheAdminPw
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,500
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1478
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: To fetch data through command line arguments

 
0
  #4
Mar 15th, 2007
It will work for the arguments you posted, but not for this
  1. c:\myprogram /id TheAdminId /pw TheAdminPw
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 19
Reputation: Masood Ali is an unknown quantity at this point 
Solved Threads: 1
Masood Ali's Avatar
Masood Ali Masood Ali is offline Offline
Newbie Poster

Re: To fetch data through command line arguments

 
0
  #5
Mar 16th, 2007
Thanks alot for the help.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC