如何在Windows上使用std :: system运行带有空格的可执行文件 [英] How to run an executable with spaces using std::system on Windows
问题描述
如何使用Windows上的std :: system不使用C ++ 11运行带有空格的可执行文件
How can an executable with spaces be run using std::system on Windows without using C++11?
尝试看起来明显的在空格中放置引号,但在控制台窗口中弹出运行命令,我收到一条消息,指示完整的可执行路径被分割在空格上。例如,我尝试了以下操作:
I've tried the seemingly obvious placing quotes around the path with spaces, but in the console window that pops up running the command I get a message that indicates that the full executable path is being split on spaces. For example, I've tried the following:
#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;
}
我验证了运行c: \\ Program Files(x86)\IronPython 2.7 \ipy-c打印'spaces_backward'
直接在cmd提示符下工作,我很确定我不只是有一个错字。
I've verified that running "c:\Program Files (x86)\IronPython 2.7\ipy" -c "print 'spaces_backward'"
directly in a cmd prompt works, and I'm pretty sure I don't just have a typo. This is driving me nuts -- any help would be greatly appreciated!
(我正在使用Visual Studio 2012,并且在子系统控制台的帮助下进行编译。)
(I'm using Visual Studio 2012 and compiling with subsystem Console if that helps.)
推荐答案
cmd.exe
的语法有一个令人讨厌的扭曲。从 cmd.exe /?
:
The syntax for cmd.exe
has a nasty twist. From 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
开关,并将命令嵌入引号中。可悲的是,它没有。这意味着这将工作:
To make the behaviour consistent, the std::system
call should use the /S
switch, and embed the command in quote marks. Sadly, it doesn't. This means that this will work:
std::system("\"c:\\Program Files (x86)\\Adobe\\Reader 10.0\\Reader\\AcroRd32.exe\" h:\\documents\\documentation\\misc\\calendar 1999.pdf");
但这个看起来不重要的变体不会:
but this seemingly trivial variant won't:
std::system("\"c:\\Program Files (x86)\\Adobe\\Reader 10.0\\Reader\\AcroRd32.exe\" \"h:\\documents\\documentation\\misc\\calendar 1999.pdf\"");
要解决此问题,请使用整个命令 / em>引用的可执行文件路径必须用引号括起来:
To fix the problem, the entire command, including the quoted path to the executable, must be surrounded in quotes:
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'\"\"");
这篇关于如何在Windows上使用std :: system运行带有空格的可执行文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!