CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    May 2000
    Location
    England
    Posts
    574

    Using Command Line Arguments...

    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


  2. #2
    Join Date
    Jun 2002
    Posts
    224
    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;
    }
    ZDF

    What is good is twice as good if it's simple.
    "Make it simple" is a complex task.

  3. #3
    Join Date
    May 2000
    Location
    Washington DC, USA
    Posts
    715
    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;
    }
    Last edited by JMS; June 20th, 2002 at 02:38 PM.

  4. #4
    Join Date
    May 2000
    Location
    England
    Posts
    574

    Command Line

    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 ?

  5. #5
    Join Date
    Feb 2000
    Location
    Rennes, France
    Posts
    624
    You can add program arguments in the menu Project/Setting, tab Debug, you can see an edit box : Program arguments.

    Marina

  6. #6
    Join Date
    Feb 2000
    Location
    Rennes, France
    Posts
    624
    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?

  7. #7
    Join Date
    May 2000
    Location
    England
    Posts
    574

    Confused ..

    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

  8. #8
    Join Date
    Feb 2000
    Location
    Rennes, France
    Posts
    624
    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

  9. #9
    Join Date
    May 2000
    Location
    England
    Posts
    574

    Thanks Marina


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured