944,082 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 3789
  • C++ RSS
Mar 15th, 2007
0

To fetch data through command line arguments

Expand Post »
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?

C++ Syntax (Toggle Plain Text)
  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...?
Similar Threads
Reputation Points: 10
Solved Threads: 1
Newbie Poster
Masood Ali is offline Offline
19 posts
since Mar 2007
Mar 15th, 2007
0

Re: To fetch data through command line arguments

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:
C++ Syntax (Toggle Plain Text)
  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.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,957 posts
since Aug 2005
Mar 15th, 2007
0

Re: To fetch data through command line arguments

Thanks for the help.
Would You please tell me that why it will not work for space, if i give arguments as
C++ Syntax (Toggle Plain Text)
  1. c:\myprogram /idTheAdminId /pwTheAdminPw
Reputation Points: 10
Solved Threads: 1
Newbie Poster
Masood Ali is offline Offline
19 posts
since Mar 2007
Mar 15th, 2007
0

Re: To fetch data through command line arguments

It will work for the arguments you posted, but not for this
C++ Syntax (Toggle Plain Text)
  1. c:\myprogram /id TheAdminId /pw TheAdminPw
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,957 posts
since Aug 2005
Mar 16th, 2007
0

Re: To fetch data through command line arguments

Thanks alot for the help.
Reputation Points: 10
Solved Threads: 1
Newbie Poster
Masood Ali is offline Offline
19 posts
since Mar 2007

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Unresolved External Symbol Error
Next Thread in C++ Forum Timeline: what's wrong?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC