当两个路径都有空格时,如何使用 C 程序中的文件启动外部程序?

发布于 2024-09-15 02:06:30 字数 829 浏览 6 评论 0原文

我正在尝试使用 VS2005 修复现有的 C 程序,该程序最终

int system(command) //in C:\Program Files\Microsoft Visual Studio 8\VC\crt\src\system.c)

使用参数值调用

start C:\Program Files\VideoLAN\VLC\vlc.exe C:\Documents and Settings\me\My Documents\My Music\09 - Track09.mp3

要启动的程序,文件名路径均由获得的环境变量组成,并且命令 start 附加到 char 缓冲区的开头。环境变量为:

  • %VLCPATH%,其值为C:\Program Files\VideoLAN\VLC

  • %MUSIC% 其值为 C:\Documents and Settings\me\My Documents \My Music

我一直在尝试这与 XP 命令提示符一起使用,当路径没有空格时一切正常。这也有效:

"%VLCPATH%\vlc.exe" "%MUSIC%\09 - Track09.mp3"

那我该怎么办?

  1. 编辑环境变量以包含引号吗? (不这么认为)
  2. 检查命令是否有文件作为参数,然后以某种方式添加带有转义字符的引号,也许它们两个并删除单词开始?
  3. 做一些我不知道的明智/优雅的事情

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

背叛残局 2024-09-22 02:06:30

我会尝试引用所有参数,例如:

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;
}

显然将可执行文件和参数替换为您确定的可执行文件和参数。

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.

ㄟ。诗瑗 2024-09-22 02:06:30

在 Windows 中,如果程序启动路径和任何带有路径名的参数包含空格,则都需要用双引号括起来(“像这样”)。

例如:

“C:\Program Files\VideoLAN\VLC\vlc.exe”“C:\Documents and Settings\me\My Documents\My Music\09 - Track09.mp3”

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"

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文