c++ - 系统函数似乎忽略了引号

标签 c++ visual-studio-2008

我正在运行以下代码(在 Windows 7 上,如果它有所不同):

char temp[20000];
sprintf_s(temp, 20000, "\"C:\\Program Files\\Mozilla Firefox\\firefox.exe\" \"http://www.tvtak.com/servlet/Gateway/?C=addShows&channel=%s&show=%s\"", _channels[chId], name);
system(temp);

运行时,控制台显示:

'C:\Program' is not recognized as an internal or external command, operable program or batch file.

'channel' is not recognized as an internal or external command, operable program or batch file.

'show' is not recognized as an internal or external command, operable program or batch file.

但是当我通过 QuickWatch 获取“temp”的值并将其粘贴到 CMD 时,它工作正常。这是怎么回事?

我应该指出,我附加到字符串的参数包含非拉丁字符。这可能与它有关吗?

最佳答案

您最好使用 CreateProcess() 来避免 cmd.exe 的引用 hell 。但是,如果您必须使用 system(),您只需将 if 1==1 附加到命令的开头,这样它就不会为您删除引号。

system("if 1==1 \"C:\Program...");

有关此问题的更多信息,请运行 cmd.exe/? 并查找 /S

关于c++ - 系统函数似乎忽略了引号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4244095/

相关文章:

c++ - 警告函数的调用者必须处理

c++ - 无需管理员身份即可在本地调试 x64 程序

c++ - Eclipse Luna GDB 调试器支持

c++ - 通过多个标签搜索数据集

c++ - boost_regex 库的静态链接

c++ - Dr.Memory 发现的错误 : don't know how to fix it

c# - VLC 播放器事件捕获

c# - 应用程序在 Visual Studio 之外卡住。从 Visual Studio 启动它时它工作

visual-studio-2008 - 如何在Visual Studio 2008中将文本编码更改为iso-8859-1

c++ - 为什么 #include 在 .h 文件或 .cpp 文件中很重要?