Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old March 14th, 2006, 09:55 AM
kitty
Guest
 
Posts: n/a
Default command line arguments

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,.

  #2  
Old March 14th, 2006, 10:05 AM
Rolf Magnus
Guest
 
Posts: n/a
Default Re: command line arguments

kitty wrote:
[color=blue]
> Can i provide for command line switches in c++ ?[/color]

Sure, why not?
[color=blue]
> For example i can say perl readfile.pl -f filename,
> Is g++ readfile.cc -f filename possible ?[/color]

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.

  #3  
Old March 14th, 2006, 10:15 AM
Jim Langston
Guest
 
Posts: n/a
Default Re: command line arguments

"kitty" <krithika.ram@gmail.com> wrote in message
news:1142329587.192117.103330@v46g2000cwv.googlegr oups.com...[color=blue]
> 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,.[/color]

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.
}


  #4  
Old March 15th, 2006, 01:35 PM
zhangliangji@gmail.com
Guest
 
Posts: n/a
Default Re: command line arguments

Thanks for everything

 

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
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 Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles