delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1997/05/08/19:17:45

From: "John M. Aldrich" <fighteer AT cs DOT com>
Newsgroups: comp.os.msdos.djgpp
Subject: Re: Help needed with command line params.
Date: Wed, 07 May 1997 23:26:32 +0000
Organization: Two pounds of chaos and a pinch of salt
Lines: 55
Message-ID: <33710FA8.A92@cs.com>
References: <336E468C DOT 35 AT avanti DOT orient DOT uw DOT edu DOT pl>
Reply-To: fighteer AT cs DOT com
NNTP-Posting-Host: ppp104.cs.com
Mime-Version: 1.0
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

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 -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019