need help with (int argc, char *argv[])

Started by
2 comments, last by cMADsc 22 years, 10 months ago
Greetings all, I need assistance on using argc, argv. I have books with them in it but they don''t describe the concept behind them. Any help is appreciated. Thanks. ----------------------------- "There are ones that say they can and there are those who actually do." "...u can not learn programming in a class, you have to learn it on your own."
-----------------------------"There are ones that say they can and there are those who actually do.""...u can not learn programming in a class, you have to learn it on your own."
Advertisement
These are used for programs that are run from the comand line. Each argument can take a value which can be used withing the program. (like those old dos programs) they can also be used when running the program from withing another program. (i think)

HHSDrum@yahoo.com
My HomepageSome shoot to kill, others shoot to mame. I say clear the chamber and let the lord decide. - Reno 911
argc is the number of command line arguements passed to your program. argv are the actual arguements.

Here''s an example

  #include <iostream>#include <stdio.h>int main(int argc, char *argv[]){	std::cout << argc << std::endl;	for(int i=0;i<argc;i++)	{		std::cout << argv[i] << std::endl;	}		return 0;}in the console or terminal>D:\test\Debug>test i am a beautiful butterfly6D:\TEST\DEBUG\TEST.EXEiamabeautifulbutterfly  


argv[0] is the program itself, so your args start at 1.

void main(int argc, char *argv[])
{
if (argc == 1)
{
printf("usage: bla.exe ");
return;
}
if (argc == 2)
{
// do something with argv[1],
// argv[0] is your executable file name
}
else
{
printf("too many arguments");
return;
}
}

pretty easy
you get the string parsed already which make it easier

Arkon
[QSoft Systems]

This topic is closed to new replies.

Advertisement