如何在 Windows 上使用 std::system 运行 带有空格的可执行文件
How to run an executable with spaces using std::system on Windows
如何在不使用 C++11 的情况下使用 Windows std::system 运行 运行
]
我已经尝试了看似明显的在路径周围放置带空格的引号,但是在控制台 window 弹出的命令中 运行 我收到一条消息,指示完整的可执行文件路径在空间上被分割。例如,我尝试了以下操作:
#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;
}
我已经验证 运行ning "c:\Program Files (x86)\IronPython 2.7\ipy" -c "print 'spaces_backward'"
直接在 cmd 提示符下工作,而且我很确定我不只是有错字。这让我抓狂——任何帮助将不胜感激!
(我正在使用 Visual Studio 2012,如果有帮助,我会使用子系统控制台进行编译。)
OK 这个代码使用 raw string literal,实际上对我有用 (VS2013 Express):
std::string cmd = R"cmd("C:\Program Files (x86)\doxygen\bin\doxygen.exe")cmd";
system(cmd.c_str());
对于不受支持的原始字符串文字,传统的转义字符串文字应如下所示 1:
std::string cmd = "\"C:\Program Files (x86)\doxygen\bin\doxygen.exe\"";
这些双重转义的原因是,system()
实际上调用了cmd
,它期望双引号"
将命令行括起来执行。
1) 很容易看出,这很容易出错,因为在某处缺少 \
。
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.
为了使行为一致,std::system
调用应使用 /S
开关,并将命令嵌入引号中。可悲的是,事实并非如此。这意味着这将起作用:
std::system("\"c:\Program Files (x86)\Adobe\Reader 10.0\Reader\AcroRd32.exe\" h:\documents\documentation\misc\calendar 1999.pdf");
但这个看似微不足道的变体不会:
std::system("\"c:\Program Files (x86)\Adobe\Reader 10.0\Reader\AcroRd32.exe\" \"h:\documents\documentation\misc\calendar 1999.pdf\"");
要解决此问题,entire 命令,包括 可执行文件的引用路径,必须用引号引起来:
std::system("\"\"c:\Program Files (x86)\Adobe\Reader 10.0\Reader\AcroRd32.exe\" \"h:\documents\documentation\misc\calendar 1999.pdf\"\"");
在你的例子中,那将是
std::system("\"\"c:\Program Files (x86)\IronPython 2.7\ipy\" -c \"print 'spaces_backward'\"\"");
如何在不使用 C++11 的情况下使用 Windows std::system 运行 运行
]我已经尝试了看似明显的在路径周围放置带空格的引号,但是在控制台 window 弹出的命令中 运行 我收到一条消息,指示完整的可执行文件路径在空间上被分割。例如,我尝试了以下操作:
#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;
}
我已经验证 运行ning "c:\Program Files (x86)\IronPython 2.7\ipy" -c "print 'spaces_backward'"
直接在 cmd 提示符下工作,而且我很确定我不只是有错字。这让我抓狂——任何帮助将不胜感激!
(我正在使用 Visual Studio 2012,如果有帮助,我会使用子系统控制台进行编译。)
OK 这个代码使用 raw string literal,实际上对我有用 (VS2013 Express):
std::string cmd = R"cmd("C:\Program Files (x86)\doxygen\bin\doxygen.exe")cmd";
system(cmd.c_str());
对于不受支持的原始字符串文字,传统的转义字符串文字应如下所示 1:
std::string cmd = "\"C:\Program Files (x86)\doxygen\bin\doxygen.exe\"";
这些双重转义的原因是,system()
实际上调用了cmd
,它期望双引号"
将命令行括起来执行。
1) 很容易看出,这很容易出错,因为在某处缺少 \
。
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.
为了使行为一致,std::system
调用应使用 /S
开关,并将命令嵌入引号中。可悲的是,事实并非如此。这意味着这将起作用:
std::system("\"c:\Program Files (x86)\Adobe\Reader 10.0\Reader\AcroRd32.exe\" h:\documents\documentation\misc\calendar 1999.pdf");
但这个看似微不足道的变体不会:
std::system("\"c:\Program Files (x86)\Adobe\Reader 10.0\Reader\AcroRd32.exe\" \"h:\documents\documentation\misc\calendar 1999.pdf\"");
要解决此问题,entire 命令,包括 可执行文件的引用路径,必须用引号引起来:
std::system("\"\"c:\Program Files (x86)\Adobe\Reader 10.0\Reader\AcroRd32.exe\" \"h:\documents\documentation\misc\calendar 1999.pdf\"\"");
在你的例子中,那将是
std::system("\"\"c:\Program Files (x86)\IronPython 2.7\ipy\" -c \"print 'spaces_backward'\"\"");