如何在Windows上使用std :: system运行带空格的可执行文件

JDi*_*teo 4 c++ windows path

如何在不使用C++ 11的情况下在Windows上使用std :: system运行带空格的可执行文件?

我尝试过看似明显的带有空格的路径周围的引号,但是在弹出运行命令的控制台窗口中,我得到一条消息,指示完整的可执行路径正在空格上分割.例如,我尝试过以下方法:

#include <cstdlib>

int main()
{
    int no_spaces_forward_rc = std::system("c:/IronPython2.7/ipy -c \"print 'no_spaces_forward'\"");
    // no_spaces_forward_rc is 0 and "no_spaces_forward" is written to console window

    int spaces_forward_rc    = std::system("\"c:/Program Files (x86)/IronPython 2.7/ipy\" -c \"print 'spaces_forward'\"");
    // spaces_forward_rc is 1, and "'c:/Program' is not recognized as an internal or external command, operable program or batch file." is written to console window

    int no_spaces_backward_rc = std::system("c:\\IronPython2.7\\ipy -c \"print 'no_spaces_backward'\"");
    // no_spaces_backward_rc is 0 and "no_spaces_backward" is written to console window

    int spaces_backward_rc    = std::system("\"c:\\Program Files (x86)\\IronPython 2.7\\ipy\" -c \"print 'spaces_backward'\"");
    // spaces_backward_rc is 1, and "'c:\Program' is not recognized as an internal or external command, operable program or batch file." is written to console window

    int no_spaces_double_backward_rc = std::system("c:\\\\IronPython2.7\\\\ipy -c \"print 'no_spaces_double_backward'\"");
    // no_spaces_double_backward_rc is 0, and no_spaces_double_backward is written to console window

    int spaces_double_backward_rc    = std::system("\"c:\\\\Program Files (x86)\\\\IronPython 2.7\\\\ipy\" -c \"print 'spaces_double_backward'\"");
    // spaces_double_backward_rc is 1, and "'c:\\Program' is not recognized as an internal or external command, operable program or batch file." is written to console window

    int spaces_double_double_backward_rc    = std::system("\\\"c:\\\\Program Files (x86)\\\\IronPython 2.7\\\\ipy\\\" -c \"print 'spaces_double_double_backward'\"");
    // spaces_dobule_double_backward_rc is 1, and "'\"c:\\Program Files (x86)\\IronPython 2.7\\ipy\"' is not recognized as an internal or external command, operable program or batch file." is written to console window

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

我已经确认"c:\Program Files (x86)\IronPython 2.7\ipy" -c "print 'spaces_backward'"直接在cmd提示符下运行,我很确定我不会只是输入错字.这让我疯了 - 任何帮助都将不胜感激!

(我正在使用Visual Studio 2012并使用子系统控制台进行编译,如果有帮助的话.)

Har*_*ton 6

语法cmd.exe有一个令人讨厌的扭曲.来自cmd.exe /?:

1.  If all of the following conditions are met, then quote characters
    on the command line are preserved:

    - no /S switch
    - exactly two quote characters
    - no special characters between the two quote characters,
      where special is one of: &<>()@^|
    - there are one or more whitespace characters between the
      two quote characters
    - the string between the two quote characters is the name
      of an executable file.
Run Code Online (Sandbox Code Playgroud)

要使行为保持一致,std::system调用应使用/S开关,并将命令嵌入引号中.可悲的是,事实并非如此.这意味着这将有效:

std::system("\"c:\\Program Files (x86)\\Adobe\\Reader 10.0\\Reader\\AcroRd32.exe\" h:\\documents\\documentation\\misc\\calendar 1999.pdf");
Run Code Online (Sandbox Code Playgroud)

但这个看似微不足道的变体不会:

std::system("\"c:\\Program Files (x86)\\Adobe\\Reader 10.0\\Reader\\AcroRd32.exe\" \"h:\\documents\\documentation\\misc\\calendar 1999.pdf\"");
Run Code Online (Sandbox Code Playgroud)

要解决此问题,整个命令(包括可执行文件的引用路径)必须用引号括起:

std::system("\"\"c:\\Program Files (x86)\\Adobe\\Reader 10.0\\Reader\\AcroRd32.exe\" \"h:\\documents\\documentation\\misc\\calendar 1999.pdf\"\"");
Run Code Online (Sandbox Code Playgroud)

在你的例子中,那就是

std::system("\"\"c:\\Program Files (x86)\\IronPython 2.7\\ipy\" -c \"print 'spaces_backward'\"\"");
Run Code Online (Sandbox Code Playgroud)