4

Is there a way to get c++ strings from the commandline like in Java?

public static void main(String[] args)

where args is an array of C++ strings?

Translucent Pain
  • 1,441
  • 2
  • 14
  • 18

7 Answers7

11

Not precisely but you can come close easily.

#include <iostream>
#include <vector>
#include <string>

using namespace std;

typedef vector<string> CommandLineStringArgs;

int main(int argc, char *argv[])
{
    CommandLineStringArgs cmdlineStringArgs(&argv[0], &argv[0 + argc]);

    for (int i = 0; i < cmdlineStringArgs.size(); ++i)
    {
        cout << cmdlineStringArgs[i] << endl;
    }

    return 0;
}

This just uses the overloaded constructor for std::vector that takes a begining/ending iterator pair to copy the command line arguments into the vectors. It is much the same as java from there on.

You can likewise build and object around that vector with utility methods to convert arguments but there is almost no point. Also there are plenty of packages with object that deal with interpreting command line switches and such. ACE, POCO, QT, etc.. all come with such facilities.

Duck
  • 26,924
  • 5
  • 64
  • 92
  • What is the purpose of first copying all arguments into a `std::vector`? The same loop could just as well iterate over `argv` directly. – sbi May 17 '11 at 22:02
  • @sbi - the point is that it answered OP's question. What he wants to accomplish is better directed to OP. – Duck May 17 '11 at 22:34
  • 1
    That's nonsense. Indexing into `argv` would have answered sean's question just as well. Nowhere was he asking how to get that arguments into a vector. You've just shown him a needlessly roundabout way. – sbi May 18 '11 at 13:11
  • 1
    He specifically wrote "where args is an array of C++ strings", not C strings. – Duck Dec 06 '11 at 14:48
  • what's the point of that `typedef`? If you ask me, it does nothing but obfuscate the code... – Jasper Feb 12 '17 at 23:59
11

You can use a vector to get the char array into strings.

#include <vector>
#include <string>
using namespace std;

int main (int argc, char** argv)
{
    vector <string> args (argv, argv + argc);
}
sbi
  • 219,715
  • 46
  • 258
  • 445
AK.
  • 743
  • 7
  • 13
  • I'd upvote this if it wouldn't be `using namespace std`. (http://stackoverflow.com/questions/2879555/c-stl-how-to-write-wrappers-for-cout-cerr-cin-and-endl/2880136#2880136) – sbi May 17 '11 at 16:44
  • 1
    @sbi That's not necessarily a bad thing to be doing inside a .cpp file. If this were a header or anything more complicated than an example 1-function file I might tend to agree with you. He is not at the same time using Boost or any other huge library, so name collisions should not be a problem. – xDD May 17 '11 at 16:54
  • This really isn't the standard way to pass arguments to a C++ program. – Pepe May 17 '11 at 16:56
  • @xDD: I disagree even in a cpp file. And I provided a link to an explanation _why_ I think so. – sbi May 17 '11 at 17:01
  • @Sevis: Indeed! I overlooked that. I went in and fixed this. It's not as if there was a controversy about that as with the other issue. – sbi May 17 '11 at 17:03
3

Yes the main function can take 2 arguments

int main(int argc, char* argv[])

Where argc is the number of arguments and argv contains a list of the arguments.

For example if you run your program as:

MyProgram.exe hello

Then

argc = 2
argv[0] = MyProgram.exe
argv[1] = "hello"
Pepe
  • 6,360
  • 5
  • 27
  • 29
1

Not built in to the language, but its very easy to implement:

#include <vector>
#include <string>
using namespace std;

int main( int argc, char *argv[] ) {
    vector <string> args;
    for ( int i = 0; i < argc; i++ ) {
        args.push_back( argv[i] );
    }
    // do something with args
}
0

In C, your main function signature looks like this:

int main(int argc, char** argv).

argc contains the number of arguments passed in by the command line. The name of the executable is in position 0, and adds one to argc.

argv contains an array of strings containing the arguments. Again, position 0 is the name of the executable.

Zach Rattner
  • 20,745
  • 9
  • 59
  • 82
0

The standard C++ main() signature is

int main(int argc, char* argv[])

where argc denotes the count of commandline arguments and argv[] is an array of primitive C strings holding the commandline arguments. argv[0] is the executable, as invoked from the commandline.

wilhelmtell
  • 57,473
  • 20
  • 96
  • 131
0

If you are writing a win32 application, you can use GetCommandLineW http://msdn.microsoft.com/en-us/library/ms683156(VS.85).aspx and CommandLineToArgW http://msdn.microsoft.com/en-us/library/bb776391(VS.85).aspx

There is a comment on the CommandLineToArg page about special handling that is needed if your executable has spaces in the path and there are no arguments that you might have to handle.

IronMensan
  • 6,761
  • 1
  • 26
  • 35