How do you execute a command line program with arguments from a c++ program? This is what I found online:
http://www.cplusplus.com/forum/general/15794/
std::stringstream stream;
stream <<"program.exe "<<cusip;
system(stream.str().c_str());
But it doesn't seem to accept an actual program location, so I am not sure how to apply this. My hope was to have something like this:
std::stringstream stream;
stream <<"C:\Tests\SO Question\bin\Release\HelloWorld.exe "<<"myargument";
system(stream.str().c_str());
This gives several warnings related to the backslashes - and the program does not work. Is it expecting you to have the program in some specific location?
This is the output I get in the console:
'C:\Tests' is not recognized as an internal or external command, operable program or batch file.
ADDENDUM:
So based on Jon's answer the correct version for me looks like this:
#include <iostream>
#include <cstdlib>
#include <sstream>
#include <cstring>
int main(int argc, char *argv[])
{
std::stringstream stream;
stream << "\"C:\\Tests\\SO Question\\bin\\Release\\HelloWorld.exe\""
<< " " // don't forget a space between the path and the arguments
<< "myargument";
system(stream.str().c_str());
return 0;
}
+operator for concatenating strings - example v.1:string myString = "something"; myString += "something else";or v.2: Directly but using typecaststring my = (string)"something" + " " +"something else";Point is: you dont have to use another library for this...HelloWorld.exe, rather than zero. That is, replace the last two statements with:return system(stream.str().c_str());