Problem using system () on Windows

I have a C ++ program that is trying to call an executable with two parameters. The code works fine on Mac, but I have some problems on Windows. I'm sure the problem is with spaces in the parameters, because when I use a path with no spaces, it works great.

Also, I print out what I send to system()

and then run that printout on the command line and it works just fine, which is a breeze.

I make the call like this: ret = system(cmd.c_str());

And if I do this: cout << cmd << endl;

I get something like this:

"C:\Program Files (x86)\MyProgram\some_executable.exe" "C:\Users\me\Desktop\files"

      

I'm not sure why the quotes don't help, I am including quotes around the paths in the call system()

. The cmd printout is exactly what I'm trying to run, but it doesn't work. However, if there were no spaces in that path, it would run just fine.

Any suggestions for passing parameters with spaces to the call system()

?

+3


source to share


1 answer


After more research, the problem is that Windows was stupid. The system call removes the first and last quote, so I had to wrap the whole thing in another set of quotes ... I found my solution here: C ++ system () doesn't work when there are spaces in two different parameters



+1


source







All Articles