Linux 使用双引号中包含空格的参数调用std::system

Linux 使用双引号中包含空格的参数调用std::system,linux,c++11,Linux,C++11,我需要使用参数调用LinuxStd::system调用,而不是使用空格来包含字符串。为了使用argc/argv正确地处理它,我想用双引号传递它 std::string cmdline=myprogram-d\I是一个sting\ 如果我不能用这根绳子,我会得到很好的结果 当我将其发送到std::systemcmdline时 看看我得到的ps-ef myprogram-d我是一个字符串 如何将I am字符串作为myprogram的单个参数 没问题: dlaru@IKH-LAPTOP /cygdri

我需要使用参数调用LinuxStd::system调用,而不是使用空格来包含字符串。为了使用argc/argv正确地处理它,我想用双引号传递它

std::string cmdline=myprogram-d\I是一个sting\

如果我不能用这根绳子,我会得到很好的结果

当我将其发送到std::systemcmdline时 看看我得到的ps-ef myprogram-d我是一个字符串

如何将I am字符串作为myprogram的单个参数

没问题:

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

在Cygwin中测试看起来很有效,我在debian上,使用g++,将测试这个程序,并让您知道测试您的程序在最后添加cin>>,然后运行ps-ef。您是对的-它可以工作,因为某些原因ps-ef从字符串中删除了,但是argv将其视为单个参数。@Dani我认为为什么“ps-ef”不显示它本身不是一个参数。