Discussion:
is it possible to pass string argv[] in main?
(too old to reply)
Pawel_Iks
2007-08-06 20:43:08 UTC
Permalink
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 ???
Victor Bazarov
2007-08-06 20:50:10 UTC
Permalink
Post by Pawel_Iks
I want to use following statement to pass command-line arguments into
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
James Kanze
2007-08-07 07:40:30 UTC
Permalink
Post by Victor Bazarov
Post by Pawel_Iks
I want to use following statement to pass command-line arguments into
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 ???
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:***@gmail.com
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
b***@gmail.com
2007-08-07 14:37:23 UTC
Permalink
Post by James Kanze
Post by Victor Bazarov
Post by Pawel_Iks
I want to use following statement to pass command-line arguments into
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 ???
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.)
--
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?
i***@gmail.com
2007-08-06 20:53:41 UTC
Permalink
Post by Pawel_Iks
Hello!
I want to use following statement to pass command-line arguments into
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::ostream_iterator<std::string>( std::cout, "\n" ));
}
Juha Nieminen
2007-08-07 15:46:41 UTC
Permalink
Post by i***@gmail.com
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);

Loading...