Qn on command line arguments to program
Mar 30, 2009 at 1:27am UTC
for c we have
int main( int argc, char* argv[] )
Is it possible to replace char* argv[] with an array of string class in c++?
like this
string argv[]?
Mar 30, 2009 at 1:31am UTC
Not really, you would have to leave the arguments as int argc and char* argv[]. You could make a string array and put them in it, but I don't think that's what you meant.
Mar 30, 2009 at 12:06pm UTC
In C++ argv is an array of char* to maintain compatibility with C so you can't use an array of strings
Mar 30, 2009 at 2:10pm UTC
I will often just do it like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
#include <iomanip>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main( int argc, char ** argv )
{
vector <string> args( argv, argv + argc );
cout << "The arguments to the program are:\n" ;
for (int i = 0; i < args.length(); i++)
cout << setw( 2 ) << right << i << ": " << args[ i ] << '\n' ;
cout << "Have a nice day!\n" ;
return 0;
}
Hope this helps.
Topic archived. No new replies allowed.