How to start an external program with a file from a C program when both paths have spaces?
I'm trying to fix an existing C-program with VS2005 that eventually calls
int system(command) //in C:\Program Files\Microsoft Visual Studio 8\VC\crt\src\system.c)
with parameter value
start C:\Program Files\VideoLAN\VLC\vlc.exe C:\Documents and Settings\me\My Documents\My Music\09 - Track09.mp3
the program to be started and the filename paths are both formed from env variables that are got and the command start is appended to start of char buffer. The env variables are:
%VLCPATH%
which has value 开发者_StackOverflowC:\Program Files\VideoLAN\VLC
%MUSIC%
which has valueC:\Documents and Settings\me\My Documents\My Music
I've been trying this with XP Command Prompt and everything works fine when paths don't have spaces. Also this works:
"%VLCPATH%\vlc.exe" "%MUSIC%\09 - Track09.mp3"
So what should I do?
- edit env variables to have quotes? (Don't think so)
- check if command has file as parameter and then somehow add quotes with escape character to maybe both of them and remove the word start?
- do something sensible / elegant that I'm not aware of
I would try quoting all of the parameters, for example:
int main(int argc, char *argv[])
{
char command[1024];
char *title = "test vlc";
char *executable = "vlc.exe";
char *param = "09 - Track09.mp3";
snprintf(command, sizeof(command), "start \"%s\" \"%s\" \"%s\"",
title, executable, param);
printf("%s\n", command);
system(command);
return EXIT_SUCCESS;
}
Obviously replace executable and param with however you determine your executable and params.
In Windows, both the program path to start and any arguments with pathnames need to be enclosed in double quotation marks ("like this") if they contain spaces.
For example:
"C:\Program Files\VideoLAN\VLC\vlc.exe" "C:\Documents and Settings\me\My Documents\My Music\09 - Track09.mp3"
精彩评论