
-----------------------------------
Geminias
Fri Jan 06, 2006 10:45 pm

command line arguments
-----------------------------------
hi friends,

how would i be able to accept command line arguments for a program?  

So if i had a program called: ROT13 and i wanted to decrypt a certain code i could just type:  ROT13 - and it would decrypt it for me and display it on screen.

-----------------------------------
wtd
Fri Jan 06, 2006 11:11 pm

Re: command line arguments
-----------------------------------
What do you think the arguments to "main" are for?

int main(int argc, char ** argv)

-----------------------------------
Geminias
Sat Jan 07, 2006 12:11 am


-----------------------------------
for anyone else whose interested in knowing:

 int main(int argc, char * argv[])

"argc" is automatically determined by how many spaces you use.
argv is a pointer to character array.  


so if you typed:  "myprog.exe argument1 argument2"
    argc = 2, because there were two spaces.  
    argv int main(int argc, char ** argv)

does the "**" mean "const char * argv"?[/quote]

-----------------------------------
wtd
Sat Jan 07, 2006 12:27 am


-----------------------------------
No, ** mean pointer to pointer.  A C-style (as opposed to C++ style) string is an array of characters terminated by the null character.  An array in C and C++ is just a glorified pointer.  Thus an array of C-style string is an array or arrays of characters.

Or... a pointer to a pointer to a character.

-----------------------------------
wtd
Sat Jan 07, 2006 12:29 am


-----------------------------------
Also, if you provide two arguments to a program, argc is 3, since there are three strings in the array.

-----------------------------------
Geminias
Sat Jan 07, 2006 12:31 am


-----------------------------------


int main (int argc, char ** argv)
{
     std::cout g++ args.cpp

C:\>a
a

-----------------------------------
Geminias
Sat Jan 07, 2006 3:09 am


-----------------------------------
ok for some reason i'm getting it now.  wierd, before it was only giving me an address.
