在 Windows 上使用 C++ 中的 system(),为啥调用另一个目录中的程序需要两个引号?

Posted

技术标签:

【中文标题】在 Windows 上使用 C++ 中的 system(),为啥调用另一个目录中的程序需要两个引号?【英文标题】:With system() in C++ on Windows, why are two quotes required to invoke a program in another directory?在 Windows 上使用 C++ 中的 system(),为什么调用另一个目录中的程序需要两个引号? 【发布时间】:2011-10-11 20:29:36 【问题描述】:

我的 utils 文件夹中有 find.exe 程序。这不起作用:

system("\"utils/find.exe\"");

我得到的只是

'utils' is not recognized as an internal or external command,
operable program or batch file.

但是,由于某种原因,这可行:

system("\"\"utils/find.exe\"\"");

回显单引号字符串

system("echo \"utils/find.exe\"");

输出

"utils/find.exe"

...那为什么我需要两个引号?

【问题讨论】:

如果你用^ 逃脱它可能会很有趣。 当问题不在于正斜杠,而是引用转义空格时,请参阅C++ system() not working when there are spaces in two different parameters 【参考方案1】:

即使您可以在 Windows 中同时使用 / 和 \ 作为目录分隔符,命令处理器也会尝试将任何以 / 开头的内容解释为开关。试试这个:

system("\"utils\\find.exe\"");

【讨论】:

【参考方案2】:

我假设您在 Windows 上,因为您正在尝试执行 .exe 文件。所以,不要写“utils/find.exe”,而是写“utils\find.exe”。 windows上的分隔符是'\',因此它可能将“utils”视为命令,因为'/'被忽略了。

【讨论】:

这就是我想到的。但是,它将是"utils\\find.exe" 是的,在 c-string 上它应该是 \\.你有没有测试过它是否可以在没有引号的情况下工作? 是的,这个问题是用 C++ 标记的。不,我没有尝试过任何一种方式。我会寻找问题的作者来做到这一点。 哦,对不起,我太困了,我没有检查你的昵称,我以为你是作者。【参考方案3】:

也许system() 正在将您的命令行传递给shell,例如cmd.exe,还需要引用吗?

【讨论】:

以上是关于在 Windows 上使用 C++ 中的 system(),为啥调用另一个目录中的程序需要两个引号?的主要内容,如果未能解决你的问题,请参考以下文章

在 Visual C++ 中的 Windows 窗体应用程序上使用 OpenGL

在 Windows 上使用 C++ 中的 Select 函数进行轮询

Windows 上 C++ Mingw 中的崩溃报告

Windows 上 C++ 中的 libxl 库设置

Windows 上 C++ 中的位图操作

“Windows 无法访问指定的设备.....” C++ 中的错误