quanrock
quanrock

Reputation: 89

_popen work incorrectly with quotation symbol

I have a block of code like this :

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

It can not work and return the result :

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

I want the _popen works like a cmd.exe.So,how can I do with this case.

Upvotes: 1

Views: 430

Answers (1)

James Reed
James Reed

Reputation: 491

Try putting a backslash before the space. I just wrote up this code snippet (I have a file called popen.exe that just prints "test") and it seems to work.

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

Upvotes: 1

Related Questions