command line arguments

Discussion in 'C++' started by kitty, Mar 14, 2006.

  1. kitty

    kitty Guest

    Can i provide for command line switches in c++ ?
    For example i can say perl readfile.pl -f filename,
    Is g++ readfile.cc -f filename possible ?

    Thankyou for all you help,.
    kitty, Mar 14, 2006
    #1
    1. Advertising

  2. kitty

    Rolf Magnus Guest

    kitty wrote:

    > Can i provide for command line switches in c++ ?


    Sure, why not?

    > For example i can say perl readfile.pl -f filename,
    > Is g++ readfile.cc -f filename possible ?


    You mean provide command line switches for your program to the compiler?
    That's not very useful, is it? The command line switches are supposed to be
    passed on execution of your program. For interpreted languages, that's the
    same time the interpreter is run, but not for compiled languages. So when
    you run you program, you can do:

    ../readfile -f filename

    If you want to provide something to the compiler that can be used in your
    program, you can use a macro. Most compilers provide a command line switch
    to define macros. In GCC, it would be -D, so you can do:

    g++ readfile.cc -DFILENAME=filename

    then you can use FILENAME within your program to refer to filename.
    Rolf Magnus, Mar 14, 2006
    #2
    1. Advertising

  3. kitty

    Jim Langston Guest

    "kitty" <> wrote in message
    news:...
    > Can i provide for command line switches in c++ ?
    > For example i can say perl readfile.pl -f filename,
    > Is g++ readfile.cc -f filename possible ?
    >
    > Thankyou for all you help,.


    Do you mean command line switches that your program can read? Yes. They
    are passed as two parameters to main. The first parameter is an integer
    giving the number of arguments. The second parameter is a pointer to an
    array of pointers to c-style strings containing the arguments.

    Example:

    #include <string>
    #include <iostream>

    int main( int argc, char* argv[] )
    {
    if ( argc < 3 )
    {
    std::cout << "Usage: myprog.exe -f <filename>" << std::endl;
    return 1;
    }

    // argv[0] is the name of the program, we don't need that
    std::string parm = argv[1];
    if ( parm != "-f" )
    {
    std::cout << "Usage: myprog.exe -f <filename>" << std::endl;
    return 1;
    }

    std::string filename = argv[2];
    // filename now contains the filename.
    }
    Jim Langston, Mar 14, 2006
    #3
  4. kitty

    Guest

    Thanks for everything
    , Mar 15, 2006
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Peter Kay
    Replies:
    1
    Views:
    473
    J├╝rgen Exner
    May 18, 2004
  2. Ahmed Moustafa

    Parser for command line arguments?

    Ahmed Moustafa, Aug 21, 2003, in forum: Java
    Replies:
    0
    Views:
    375
    Ahmed Moustafa
    Aug 21, 2003
  3. SC
    Replies:
    2
    Views:
    646
    Michiel Salters
    May 5, 2004
  4. Ashe Corven

    command line arguments

    Ashe Corven, May 8, 2004, in forum: C++
    Replies:
    3
    Views:
    516
    Francis Glassborow
    May 8, 2004
  5. Replies:
    4
    Views:
    1,691
    Terry Hancock
    Apr 23, 2005
Loading...

Share This Page