Can't call system with white space in parameter

148 Views Asked by At

I'm trying to make this system call work. it works in case there is no space in address, but it doesn't work if there's a space in address...

this call copies a file from one place to another.

how to escape space in this code properly?

char buffer[300];
snprintf(buffer, sizeof(buffer), "copy %s\\%s %s", AssistPath, apiFileName, path);
system(buffer);
2

There are 2 best solutions below

7
On

Space is matter? Then why don't you try this code?

snprintf(buffer, sizeof(buffer), "copy \"%s\\%s\" \"%s\"", AssistPath, apiFileName, path);
0
On

You can probably use quotes around the things that may have spaces

"copy \"%s\\%s\" \"%s\""