【问题标题】:_popen work incorrectly with quotation symbol_popen 使用引号符号不正确
【发布时间】:2013-11-15 22:17:58
【问题描述】:

我有这样一段代码:

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

它不能工作并返回结果:

'd:\a' 不是内部或外部命令、可运行程序或批处理文件。

但是当我在第二个参数处将引号 " 更改为 ' => 没关系

\"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 一样工作。那么,我该如何处理这种情况。

【问题讨论】:

    标签: c++ command execute popen


    【解决方案1】:

    尝试在空格前加一个反斜杠。我刚刚编写了这段代码 sn-p(我有一个名为 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;
    }
    

    【讨论】:

    • 是的,只需打开一个像您的示例一样的文件,它就可以正常工作。但是如果你用文件A打开文件B,就会报错。
    • 将整个命令用引号引起来pFile = _popen("\"\"C:\\test 1\\popen.exe\" \"C:\\test 1\\popen.txt\"\" ", "r");
    猜你喜欢
    • 2011-01-26
    • 2012-01-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多