tags:

views:

132

answers:

2

Hi,

I have this windows console app which takes a file, do some calculations, and then writes the output to a specified file. The input is specified in "app.exe -input fullfilename" format. I need to call this application from my C++ program, but I have a problem with spaces in paths to files. When I call the app directly from cmd.exe by typing (without specifying output file for clarity)

"c:\first path\app.exe" -input "c:\second path\input.file"

everything works as expected. But, when I try using cstdlib std::system() function, i.e.

std::system(" \"c:\\first path\\app.exe\" -input \"c:\\second path\\input.file\" ");

the console prints out that c:\first is not any valid command. It's probably common mistake and has simple solution, but I have been unable to find any. Thx for any help.

A: 

Don't try to put the quotes in the std::system() call. Try the following:

std::system("c:\\first\\ path\\app.exe -input c:\\second\\ path\\input.file");
Amardeep
+1  A: 

Instead of std::system(), you should use the _wspawnv function from the Windows API. Use _wspawnvp if you want to search for the program in PATH, rather than specifying a full path to it.

#include <stdio.h>
#include <wchar.h>
...
const WCHAR *app = L"C:\\path to\\first app.exe";
const WCHAR *argv[] = {app, L"-input", L"c:\\second path\\input file.txt"};
_wpspawnv(_P_WAIT, app, argv);

You could also use _spawnv / _spawnvp if you are 100% sure that your input filename will never, ever contain anything else than ASCII.

Krzysztof Kosiński