Archived

This topic is now archived and is closed to further replies.

cMADsc

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

Recommended Posts

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."

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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 butterfly
6
D:\TEST\DEBUG\TEST.EXE
i
am
a
beautiful
butterfly


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

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites