3

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 issues on Windows. I'm confident that the issue is related to spaces in the parameters because when I use a path with no spaces it works just fine.

Furthermore, I print out what I'm sending into system() and then I run that printout on the command line and it works just fine which is trifling.

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

And if I do: cout << cmd << endl; I'll get something like this:

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

I'm not sure why the quotes aren't helping, I am including quotes around the paths in the system() call. The printout of cmd is exactly what I'm trying to run but it doesn't work. However, if that path had no spaces in it, it would execute just fine.

Any suggestions on passing parameters with spaces to a system() call?

4
  • What does system return? If -1 what error is set by errno? Commented Feb 19, 2013 at 15:53
  • It doesn't return -1, I have a catch just after that block of code to see what ret is...it returns 1. However, I can tell if it ran correctly because some_executable.exe will output a file. Commented Feb 19, 2013 at 15:58
  • 2
    Strange. system() returning 1 means it succeeded in invoking your program, and the program itself failed with exit code 1. Commented Feb 19, 2013 at 16:01
  • I agree, strange. But I'm convinced it revolves around the spaces in the path. Remove the spaces it works, add spaces, then it stops working. Working meaning, outputting the file I'm looking for. Commented Feb 19, 2013 at 16:10

1 Answer 1

1

After more research, the issue is related to Windows being 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() not working when there are spaces in two different parameters

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.