I need to call a linux std::system call with parameter than contain string with spaces. In order to process it correctly using argc / argv I want to pass it with double quotes.
std::string cmdline = “myprogram -d “I am a sting”” ;
if I cout this string I get good results.
When I send it to std::system(cmdline)
and look at ps -ef I get
myprogram -d I am a string
How can I keep “I am a string” as a single parameter for myprogram ?
Advertisement
Answer
No problem:
dlaru@IKH-LAPTOP /cygdrive/c/Users/dlaru $ cat test.cpp #include <iostream> #include <string> #include <cstdlib> int main() { std::string cmdline = "./see "I am a string""; std::cout << cmdline << std::endl; std::system(cmdline.c_str()); } dlaru@IKH-LAPTOP /cygdrive/c/Users/dlaru $ cat see.cpp #include <iostream> int main(int argc, char* argv[]) { for (int i = 0; i < argc; ++i) { std::cout << argv[i] << std::endl; } } dlaru@IKH-LAPTOP /cygdrive/c/Users/dlaru $ g++ -std=c++11 test.cpp -o test dlaru@IKH-LAPTOP /cygdrive/c/Users/dlaru $ g++ -std=c++11 see.cpp -o see dlaru@IKH-LAPTOP /cygdrive/c/Users/dlaru $ ./test ./see "I am a string" ./see I am a string
(tested in Cygwin)