旁邊的事實,有可能更優雅的解決方案不是通過C++執行PROGRAMM你可以用其中的一個去調用終端:
的std :: string
的最明顯的解決方案是使用std::string
,它提供了重載運算符+
來連接字符串。
#include <string>
std::string args = "gnome-terminal ";
args += "-e 'sh ./spout";
args += "' ";
的std :: stringstream的
std::stringstream
是另一種選擇:
#include <sstream>
#include <string>
std::stringstream ss;
ss << "gnome-terminal ";
ss << "-e 'sh ./spout";
ss << "' ";
std::string args = ss.str();
的strcat()
如果你想使用C字符串,你可以使用像這個。請注意,我不建議這樣做。
#include <cstring>
strcpy(args, "gnome-terminal");
strcat(args, "-e 'sh ./spout");
strcat(args, "' ");
請注意,第二個版本需要在爲args
所分配的內存一探究竟。有關更多信息,請參閱strcat()。
第二個'strcpy'會覆蓋第一個'strcpy'。你爲什麼不使用'std :: string?' – Quentin