【问题标题】:Can't call system with white space in parameter无法调用参数中有空格的系统
【发布时间】:2015-06-19 12:10:28
【问题描述】:

我正在尝试使这个系统调用工作。 在地址没有空格的情况下有效,但如果地址中有空格则无效...

此调用将文件从一个地方复制到另一个地方。

如何正确转义这段代码中的空格?

char buffer[300];
snprintf(buffer, sizeof(buffer), "copy %s\\%s %s", AssistPath, apiFileName, path);
system(buffer);

【问题讨论】:

  • @Dummy00001,我没有时间阅读所有内容,也许你可以将它应用到我的代码中??如果您已经知道正确的方法并有兴趣帮助我...
  • 有人请帮助我.....
  • 发布 code 看到的 AssistPath, apiFileName, pathprintf("AssistPath:[%s], apiFileName:[%s], path:[%s]\n", AssistPath, apiFileName, path) 的值,printf("AssistPath:[%s], apiFileName:[%s], path:[%s]\n", AssistPath, apiFileName, path) 使用 [] 帮助识别字符串的真正开始/结束。怀疑您的变量分配存在其他问题。

标签: c batch-file system whitespace


【解决方案1】:

空间很重要?那你为什么不试试这段代码呢?

snprintf(buffer, sizeof(buffer), "copy \"%s\\%s\" \"%s\"", AssistPath, apiFileName, path);

【讨论】:

  • @ufosecret 你能告诉我们你的AssistPathapiFileNamepath的测试用例吗?
  • D:\tt t\assist\api.dll D:\new
  • 先是辅助路径 + 文件名,然后是新路径
  • @ufosecret 你的意思是AssistPath = "D:\\tt"apiFileName = "t\\assist\\api.dll"path = "D:\new"?那么他们就没有空间了,是吗?
  • 在 snprintf 调用之后,添加 printf("buffer = %s\n",buffer); 并查看输出是什么。这应该可以揭示问题。
【解决方案2】:

你可以在可能有空格的东西周围使用引号

"copy \"%s\\%s\" \"%s\""

【讨论】:

    猜你喜欢
    • 2011-02-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-25
    • 2016-08-30
    • 1970-01-01
    相关资源
    最近更新 更多