Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > command line arguments

Reply
Thread Tools

command line arguments

 
 
kitty
Guest
Posts: n/a
 
      03-14-2006
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,.

 
Reply With Quote
 
 
 
 
Rolf Magnus
Guest
Posts: n/a
 
      03-14-2006
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.

 
Reply With Quote
 
 
 
 
Jim Langston
Guest
Posts: n/a
 
      03-14-2006
"kitty" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
> 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.
}


 
Reply With Quote
 
zhangliangji@gmail.com
Guest
Posts: n/a
 
      03-15-2006
Thanks for everything

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Run Unix shell command $ parse command line arguments in python rkoida@yahoo.com Python 4 04-23-2005 04:42 AM
Documentation on command line arguments to perl Peter Kay Perl 1 05-18-2004 01:27 AM
command line arguments Ashe Corven C++ 3 05-08-2004 06:56 PM
How to hide command line arguments in a windows application SC C++ 2 05-05-2004 10:06 AM
Parser for command line arguments? Ahmed Moustafa Java 0 08-21-2003 05:21 AM



Advertisments