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

P

Pawel_Iks

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 ???
 
V

Victor Bazarov

Pawel_Iks said:
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
 
I

int2str

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 ));

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

James Kanze

Pawel_Iks said:
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.)
 
B

bandikiller07

Pawel_Iks said:
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:[email protected]
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?
 
J

Juha Nieminen

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

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


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);
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top