How to call popen() with a pathname containing spaces under Windows in C/C++?

c, c++, popen, quoting

Solution

It turns out the popen function strips the quotation marks around the whole thing, eg. `"C:/Program Files"` becomes `C:/Program Files` and `"Ab cd.exe" "ef gh"` becomes `ab cd" "ef gh`. You can get around this by just adding quotation marks around the whole thing, eg. `""ab cd.exe" "ef gh""` which becomes `"ab cd.exe" "ef gh"` as intended.

This solution was inspired by reply from Kaz

Problem

I'm trying to call popen() using mingw like this: ``` #define RUN_COMMAND "\"C:\\Program Files\\CA\\BrightStor ARCserve Backup\\ca_qmgr.exe\" \"-list\"" int main() { outputPointer = popen(RUN_COMMAND, "r"); ... } ``` But I can't get it working. I think it's a quoting nightmare ...

Original source

Related problems