By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,677 Members | 1,069 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,677 IT Pros & Developers. It's quick & easy.

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

P: n/a
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 ???

Aug 6 '07 #1
Share this Question
Share on Google+
5 Replies


P: n/a
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::stringsargv(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
Aug 6 '07 #2

P: n/a
On Aug 6, 1:43 pm, Pawel_Iks <pawel.labed...@gmail.comwrote:
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::ostream_iterator<std::string>( std::cout, "\n" ));
}

Aug 6 '07 #3

P: n/a
On Aug 6, 10:50 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
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::stringsargv(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:ja*********@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

Aug 7 '07 #4

P: n/a
On Aug 7, 12:40 pm, James Kanze <james.ka...@gmail.comwrote:
On Aug 6, 10:50 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:


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::stringsargv(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:james.ka...@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- Hide quoted text -

- Show quoted text -
are you learning in niit?

Aug 7 '07 #5

P: n/a
in*****@gmail.com 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);
Aug 7 '07 #6

This discussion thread is closed

Replies have been disabled for this discussion.