7

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;
}
6
  • Any idea how to redirect stdout from system(...) to std::string or std::stringstream? Commented Jul 3, 2014 at 11:25
  • 1
    @Pupsik The system function returns an int. Apparently you would need to know the status codes for whatever operating system you are using: "If command is not a null pointer, the value returned depends on the system and library implementations, but it is generally expected to be the status code returned by the called command, if supported." - from cplusplus.com/reference/cstdlib/system - then you could produce the proper error message based on the int returned. Commented Jul 8, 2014 at 21:39
  • 1
    @Pupsik No. I was talking to the other Pupsik. Johnny Pupsik. He's from Ireland. Commented Jul 10, 2014 at 14:12
  • 1
    PS: C++ actually has a + operator for concatenating strings - example v.1: string myString = "something"; myString += "something else"; or v.2: Directly but using typecast string my = (string)"something" + " " +"something else"; Point is: you dont have to use another library for this... Commented Oct 19, 2014 at 22:41
  • 1
    In a program such as this, you would want to return the exit value of HelloWorld.exe, rather than zero. That is, replace the last two statements with: return system(stream.str().c_str()); Commented Oct 22, 2014 at 9:58

2 Answers 2

10

First of all, you should use double backslashes in literal strings whenever you want a single backslash to appear in the actual string value. This is according to the language grammar; a conforming compiler could do worse than simply warning about this.

In any case, the problem you are experiencing is due to the fact that paths containing spaces must be enclosed in double quotes in Windows. Since the double quotes themselves need to be escaped inside a C++ string literal, what you need to write is

stream << "\"C:\\Tests\\SO Question\\bin\\Release\\HelloWorld.exe\""
       << " " // don't forget a space between the path and the arguments
       << "myargument";
Sign up to request clarification or add additional context in comments.

Comments

5

This gives several warnings related to the backslashes

I believe \ is an escape character in C++ using \\ instead will probably solve this problem.

1 Comment

Also your code will pass the string "myarguments" instead of a var containing this info as written - which may be as intended.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.