Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c++

c

quoting

popen

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 ...

like image 312
GabrieleV Avatar asked Oct 12 '09 21:10

GabrieleV


1 Answers

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

like image 55
Peter Harmann Avatar answered Oct 03 '22 09:10

Peter Harmann