如果我想輸入需要路徑的命令時,我的路徑中有空格,如何在C++中使用system()函數? 示例代碼:帶空格的C++系統()命令路徑
#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;
}
因爲外殼中的空間的前需要退格這並不推測工作。 不幸的是,這並不工作過:
std::string command = "ls -la /home/testuser/this\b is\b a\b folder/test/";
任何想法,我做錯了或者我怎麼可能做的更好?謝謝。
爲什麼使用「鍾」字符'\ b'?你需要反斜槓'「a \\ b」'或者把你的路徑放到一個單引號中。請注意,這兩種方法仍然容易受到流氓反斜槓或報價的影響,所以爲了逃避每一個不好的字符,您需要像Python – myaut
的'pipes.quote'這樣的單個'\\'''''' – Raindrop7
謝謝!有用。我只是誤解了\ b。它被寫爲「退格」,我沒有閱讀全文。愚蠢的我... https://www.daniweb.com/programming/software-development/threads/230885/what-is-the-purpose-of-using-r-and-b-in-c – Dominik