旁边的事实,有可能更优雅的解决方案不是通过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