开发者

How to start an external program with a file from a C program when both paths have spaces?

开发者 https://www.devze.com 2023-01-11 21:43 出处:网络
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)

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 value C:\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?

  1. edit env variables to have quotes? (Don't think so)
  2. 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?
  3. 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"

0

精彩评论

暂无评论...
验证码 换一张
取 消