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?

for (int i = 0; i < argc; i++)

if (strnicmp(argv[i],"/id",3) == 0)
      {
         strncpy(ID,argv[i] + 3,40);    //ID is defined as a char array of size 40
      }

kindly tell me how does it work...?

Recommended Answers

All 4 Replies

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 + 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:\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.

Thanks for the help.
Would You please tell me that why it will not work for space, if i give arguments as

c:\myprogram /idTheAdminId /pwTheAdminPw

It will work for the arguments you posted, but not for this

c:\myprogram /id TheAdminId /pw TheAdminPw

Thanks alot for the help.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.