Mail Archives: djgpp/1997/05/08/19:17:45
Piotr Sieklucki wrote:
>
> Hello fellow DJGPPers,
>
> I would like to ask for some advice. How do you read the command-line
> parameters given to a program at runtime, and copy them to strings?
> Basically, I want to get the 2nd and 4th (given that the 1st is the
> full pathname to the program) parameters into strings <2nd> and <4th>.
> I realise that this is bloody simple, but I just seem to be bloody
> stupid. Anyone out there who can give me a helping hand, plz? TIA.
ANSI C states that the command line arguments given to your program are
passed as parameters to the main() function like so:
int main( int argc, char **argv );
argc contains the number of arguments, and argv is an array of strings
containing their values. argv[0] is always the name that the program
was invoked by, so the actual parameters start with argv[1]. So a
standard method of reading the command line would look like this:
int main( int argc, char **argv )
{
int i;
for ( i = 1; i < argc; i++ )
{
if ( argv[i][0] == '-' )
switch( tolower( argv[i][1] ) )
{
case '?':
case 'h':
display_help( );
break;
case 'v':
flag_verbose = 1;
break;
}
else
process_file( argv[i] );
}
return 0;
}
--
John M. Aldrich, aka Fighteer I <fighteer AT cs DOT com>
-----BEGIN GEEK CODE BLOCK-----
Version: 3.1
GCS d- s+:- a-->? c++>$ U@>++$ p>+ L>++ E>++ W++ N++ o+>++ K? w(---)
O- M-- V? PS+ PE Y+ PGP- t+(-) 5- X- R+(++) tv+() b+++ DI++ D++ G>++
e(*)>++++ h!() !r !y+()
------END GEEK CODE BLOCK------
- Raw text -