Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > is it possible to pass string argv[] in main?

Reply
Thread Tools

is it possible to pass string argv[] in main?

 
 
Pawel_Iks
Guest
Posts: n/a
 
      08-06-2007
Hello!

I want to use following statement to pass command-line arguments into
main function:

int main(int argc, string argv[])
{
if (argc>0)
{
//do something
}

return 0;
}

and it's an error ... do i have to use *char type, and if yes, how to
convert it into string type which is more diserable for me ... any
clues, solutions ???

 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      08-06-2007
Pawel_Iks wrote:
> I want to use following statement to pass command-line arguments into
> main function:
>
> int main(int argc, string argv[])
> {
> if (argc>0)
> {
> //do something
> }
>
> return 0;
> }
>
> and it's an error ... do i have to use *char type, and if yes, how to
> convert it into string type which is more diserable for me ... any
> clues, solutions ???


Construct a vector of strings:

if (argc > 0) {
std::vector<std::string> sargv(argv, argv + argc);
// do something with 'sargv'
}

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


 
Reply With Quote
 
 
 
 
int2str@gmail.com
Guest
Posts: n/a
 
      08-06-2007
On Aug 6, 1:43 pm, Pawel_Iks <(E-Mail Removed)> wrote:
> Hello!
>
> I want to use following statement to pass command-line arguments into
> main function:
>
> int main(int argc, string argv[])
> {
> if (argc>0)
> {
> //do something
> }
>
> return 0;
>
> }
>
> and it's an error ... do i have to use *char type, and if yes, how to
> convert it into string type which is more diserable for me ... any
> clues, solutions ???


Yes, you have to use char**.
You can convert it like so:

#include <iostream>
#include <vector>
#include <algorithm>
#include <iterator>
#include <string>

int main( int argc, char* argv[] )
{
std::vector< std::string > args;

for( int i = 0; i != argc; ++i )
args.push_back( std::string( argv[i] ));

std::copy( args.begin(), args.end()
, std:stream_iterator<std::string>( std::cout, "\n" ));
}

 
Reply With Quote
 
James Kanze
Guest
Posts: n/a
 
      08-07-2007
On Aug 6, 10:50 pm, "Victor Bazarov" <(E-Mail Removed)> wrote:
> Pawel_Iks wrote:
> > I want to use following statement to pass command-line arguments into
> > main function:


> > int main(int argc, string argv[])
> > {
> > if (argc>0)
> > {
> > //do something
> > }


> > return 0;
> > }


> > and it's an error ... do i have to use *char type, and if yes, how to
> > convert it into string type which is more diserable for me ... any
> > clues, solutions ???


> Construct a vector of strings:


> if (argc > 0) {
> std::vector<std::string> sargv(argv, argv + argc);
> // do something with 'sargv'
> }


Just for the record, you don't need the if; argc is guaranteed
to be >= 1. (If the name of the program isn't available---never
the case under Windows or Unix---, then argv[0] is the empty
string.)

--
James Kanze (GABI Software) email:(E-Mail Removed)
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

 
Reply With Quote
 
bandikiller07@gmail.com
Guest
Posts: n/a
 
      08-07-2007
On Aug 7, 12:40 pm, James Kanze <(E-Mail Removed)> wrote:
> On Aug 6, 10:50 pm, "Victor Bazarov" <(E-Mail Removed)> wrote:
>
>
>
>
>
> > Pawel_Iks wrote:
> > > I want to use following statement to pass command-line arguments into
> > > main function:
> > > int main(int argc, string argv[])
> > > {
> > > if (argc>0)
> > > {
> > > //do something
> > > }
> > > return 0;
> > > }
> > > and it's an error ... do i have to use *char type, and if yes, how to
> > > convert it into string type which is more diserable for me ... any
> > > clues, solutions ???

> > Construct a vector of strings:
> > if (argc > 0) {
> > std::vector<std::string> sargv(argv, argv + argc);
> > // do something with 'sargv'
> > }

>
> Just for the record, you don't need the if; argc is guaranteed
> to be >= 1. (If the name of the program isn't available---never
> the case under Windows or Unix---, then argv[0] is the empty
> string.)
>
> --
> James Kanze (GABI Software) email:(E-Mail Removed)
> Conseils en informatique orientée objet/
> Beratung in objektorientierter Datenverarbeitung
> 9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34- Hide quoted text -
>
> - Show quoted text -


are you learning in niit?

 
Reply With Quote
 
Juha Nieminen
Guest
Posts: n/a
 
      08-07-2007
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> int main( int argc, char* argv[] )
> {
> std::vector< std::string > args;
>
> for( int i = 0; i != argc; ++i )
> args.push_back( std::string( argv[i] ));


Why do it like that when you can do it more easily like:


int main( int argc, char* argv[] )
{
std::vector< std::string > args(argv, argv+argc);
 
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
Is this possible: map of references (map<string&, string&>) asclearuc@gmail.com C++ 3 05-12-2007 03:31 PM
difference between pass by address and pass by reference!! blufox C Programming 2 04-03-2006 02:53 PM
Pass by reference / pass by value Jerry Java 20 09-09-2005 06:08 PM
Pass-by-reference instead of pass-by-pointer = a bad idea? Mr A C++ 111 07-14-2005 03:04 AM
string !~ string, not possible in cvshead Simon Strandgaard Ruby 2 04-01-2004 10:08 AM



Advertisments