Click to See Complete Forum and Search --> : Using Command Line Arguments...
posty68
June 20th, 2002, 03:15 AM
Hi
I am working through Practical C++ Book by Rob mcgregor and he has a section concerning "Using command line arguments"
its not very well explained and i dont really understand what he is saying .
Can anybody offer me some advice/knowledge on this matter with some examples i could learn from ...
many thanks
P
:rolleyes:
zdf
June 20th, 2002, 05:32 AM
Try to read Bjarne Stroustup’s <The C++ Programming Language>.
The snippet below may help you (although you’ll find some Romanian words). The code shows you how to parse the command line. It may need some changes since the original program runs under DOS.
void error( int ec, char * s1 = "", char * s2 = 0 )
{
cerr << "eroare : " << s1;
if( s2 )
cerr << " : "<< s2;
cerr << endl;
exit(ec);
}
void help( char* argv0 )
{
char progName[MAXFILE];
fnsplit( argv0, 0, 0, progName, 0 );
cout << "Song Generator Version 1.0. Copyright (c) 1998 Florin Dumitrescu." << endl
<< endl
<< "Sintaxa: " << progName << " [-optiuni] fisier_sursa [fisier_destinatie]" << endl
<< endl
<< " optiuni:" << endl
<< " -p canta" << endl
<< " -c genereaza fiser sursa \"c\"" << endl
<< " -tn canta/genereaza in tempo-ul n[ms]" << endl
<< " -bn canta/genereaza cu frecventa de baza n[Hz]" << endl
<< " optiunile c si p sunt incompatibile" << endl
<< endl
<< " extensiile implicite:" << endl
<< " .sg fisier sursa" << endl
<< " .bin fisier destinatie binar" << endl
<< " .c fisier destinatie \"c\"" << endl
<< " nu sunt permise descrieri generale (* si ?)" << endl;
}
int main( int argc, char ** argv )
{
if ( argc <= 1 )
{
help(argv[0]);
return 1;
}
char c;
_base = _dflt_base;
while (--argc > 0 && (*++argv)[0] == '-')
while ((c = toupper(*++argv[0])) != 0)
switch (c)
{
case 'C': gen_c_code = 1;
break;
case 'P': play = 1;
break;
case 'T': {
char s[128], *p;
p = s;
while (isdigit(*p = *++argv[0]) && (*p++ != 0));
--argv[0];
*p = 0;
if (!strlen(s) || !(_tempo=_user_tempo= atoi(s)))
error( 1, "optiunea t are nevoie de un numar" );
}
break;
case 'B': {
char s[128], *p;
p = s;
while (isdigit(*p = *++argv[0]) && (*p++ != 0));
--argv[0];
*p = 0;
if (!strlen(s) || !(_base=_user_base= atoi(s)))
error( 1, "optiunea b are nevoie de un numar" );
}
break;
default : char * es = " : optiune necunoscuta";
es[0] = c;
error( 1, es);
}
if ( gen_c_code && play )
error( 1, "c si p: optiuni incompatibile" );
if ( argc < 1 )
error( 1, "argumente insuficiente");
if ( (play && argc > 1) || argc > 2)
error( 1, "prea multe argumente" );
//...
return 0;
}
JMS
June 20th, 2002, 02:35 PM
If you call the demo "cmdline" program like so...
cmdline a b c d e f g
You will get the following output
The absolute name of the EXE file is always parameter 0 or [C:\demo\cmdline\Debug\cmdline.exe]
Argument 1 = [a]
Argument 2 = [b]
Argument 3 = [c]
Argument 4 = [d]
Argument 5 = [e]
Argument 6 = [f]
Argument 7 = [g]
** Note 'a' is argument 1 and not argument 0 as one might expect..
Here is the code ( Sorry about the spacing.. indents on this board still don't work very well )
// cmdline.cpp
int main(int argc, char* argv[])
{
// argv[0] doesn't change even if their are or aren't other parameters
printf( "The absolute name of the EXE file is always parameter 0 or [%s]\n", argv[0] );
// So your actual paramerts start at 1 instead of 0 as you might expect
for ( int i = 1; i < argc; i++ )
{
printf( "Argument %d = [%s]\n", i, argv[i] );
}
return 0;
}
posty68
June 21st, 2002, 03:37 AM
So once the code is typed in and built
how to i get the program to run
do i have to bring up the command prompt ?
Marina Vaillant
June 21st, 2002, 04:03 AM
You can add program arguments in the menu Project/Setting, tab Debug, you can see an edit box : Program arguments.
Marina
Marina Vaillant
June 21st, 2002, 04:04 AM
Sorry that was "non -visual C++" issue).
then yes, if you run your program by typing its name, you put your program arguments right after it... I must be missing something here. It that really your question?
posty68
June 21st, 2002, 04:07 AM
Hi Yes that is my question .
im not to good at DOS ????
i know real basics like cd is change directory ...
so when the program is built
i type in the path to the .exe and then add the parameters after ??
thanks
Marina Vaillant
June 21st, 2002, 04:14 AM
Yes exactly.
You separate the program name and its argument by a space, and each argument if there are several.
Try, you'll learn so.
Marina
posty68
June 21st, 2002, 04:16 AM
:)
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.