【发布时间】:2017-10-07 13:59:02
【问题描述】:
如果我想在路径中有空格的情况下输入需要路径的命令,如何在 c++ 中使用 system() 函数? 示例代码:
#include <iostream>
int main()
{
std::string command = "ls -la /home/testuser/this is a folder/test/";
std::cout << "Click enter to execute command..." << std::endl;
getchar();
std::system(command.c_str());
return 0;
}
据说这不起作用,因为 shell 需要在空格前使用退格键。 不幸的是,这也不起作用:
std::string command = "ls -la /home/testuser/this\b is\b a\b folder/test/";
知道我做错了什么或如何做得更好吗?谢谢。
【问题讨论】:
-
为什么要用“铃”字
\b?您需要使用反斜杠"a\\ b"或将路径放入单引号'a b'。请注意,这两种方法仍然容易受到流氓反斜杠或引号的影响,因此要转义每个坏字符,您需要 Python 中的pipes.quote之类的东西 -
\\` for a single\` -
谢谢!有用。我只是误解了\ b。它被写成“退格”,我没有阅读全文。愚蠢的我...daniweb.com/programming/software-development/threads/230885/…
标签: c++ string function system