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

如何在Windows上使用std :: system使用空格运行可执行文件,第1张

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

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

我已经尝试了空间的path看似明显的引号,但在popup的控制台窗口运行命令我得到一个消息,指示完整的可执行文件path被拆分空间。 例如,我已经尝试了以下内容:

#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; }

我已经valIDation了直接在cmd提示符下运行"c:Program files (x86)IronPython 2.7ipy" -c "print 'spaces_backward'" ,我很确定我不只是有一个错字。 这是驱使我坚果 – 任何帮助将不胜感激!

(我正在使用Visual Studio 2012和编译与子系统控制台,如果有帮助。)

将.NET应用程序添加到registry以在重新启动后启动以完成某些 *** 作

“顶级”程序用于在屏幕上重新显示数据的技术是什么?

如何将windows位图转换为C + +中的ActionScript脚本

无法访问“程序文件”中的INI文件

构buildbinutils时使用了哪些configuration选项?

只清除控制台输出的一部分

从GtkWidget获取X11窗口句柄

为什么套接字读取的数据比实际发送的数据多?

重新启动分叉进程中的线程

在windows中拦截库函数最简单的方法是什么?

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"");

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

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'""");

确定这个代码使用原始字符串文字 ,实际上适用于我(VS2013 Express):

std::string cmd = R"cmd("C:Program files (x86)doxygenbindoxygen.exe")cmd"; system(cmd.c_str());

对于不受支持的原始字符串文字,传统上转义的字符串文字应如下所示1:

std::string cmd = ""C:\Program files (x86)\doxygen\bin\doxygen.exe"";

这些双重转义的原因是, system()实际上调用了需要双引号的cmd来"封装要执行的命令行" 。

1)很容易看出,这是错误的错误的地方。

总结

以上是内存溢出为你收集整理的如何在Windows上使用std :: system使用空格运行可执行文件全部内容,希望文章能够帮你解决如何在Windows上使用std :: system使用空格运行可执行文件所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/1154753.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-01
下一篇 2022-06-01

发表评论

登录后才能评论

评论列表(0条)

保存