gpt4 book ai didi

c++ - _popen 使用引号时工作不正确

转载 作者:太空宇宙 更新时间:2023-11-04 11:46:08 24 4
gpt4 key购买 nike

我有一段这样的代码:

FILE * file = _popen("\"d:\\a b\\sql.exe\" \"d:\\a b\\la.db\"", "r");
_pclose(file);

它不能运行并返回结果:

'd:\a' is not recognized as an internal or external command, operable program or batch file.

But when I change quotation " to ' at second paramester => It's OK

\"d:\\a b\\la.db\"" =>  \'d:\\a b\\la.db\'"
FILE * file = _popen("\"d:\\a b\\sql.exe\" \'d:\\a b\\la.db\'", "r");
_pclose(file);

我希望 _popen 像 cmd.exe 一样工作。那么,我该如何处理这种情况。

最佳答案

尝试在空格前加一个反斜杠。我刚刚编写了这个代码片段(我有一个名为 popen.exe 的文件,它只打印“test”)并且它似乎可以工作。

#include <cstdio>
#include <cstdlib>

int main()
{
FILE * pFile;
char buffer[100];

pFile = _popen("\"C:\\test\ 1\\popen.exe\"", "r");
if (pFile == NULL) perror("Error opening file");
else
{
while (!feof(pFile))
{
if (fgets(buffer, 100, pFile) == NULL) break;
fputs(buffer, stdout);
}
_pclose(pFile);
}
return 0;
}

关于c++ - _popen 使用引号时工作不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19782249/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com