阅读量:2
在C++中与Linux命令行进行交互,通常涉及到执行外部命令并处理其输出。这可以通过多种方式实现,包括使用标准库函数、系统调用或者第三方库。以下是一些常用的方法和技巧:
system()函数system()是一个简单的调用,可以用来执行shell命令。它的原型在头文件中定义。
#include
int main() {
int ret = system("ls -l");
return ret;
}
注意:system() 函数会阻塞直到命令执行完毕,并且它不能很好地处理命令的输出。
popen()和pclose()函数popen()函数可以打开一个指向命令的管道,并允许你读取命令的输出或向其写入数据。pclose()用于关闭这个管道。
#include
#include
#include
#include
std::string exec(const char* cmd) {
std::array<char, 128> buffer;
std::string result;
std::shared_ptr pipe(popen(cmd, "r"), pclose) ;
if (!pipe) throw std::runtime_error("popen() failed!");
while (fgets(buffer.data(), buffer.size(), pipe.get()) != nullptr) {
result += buffer.data();
}
return result;
}
int main() {
std::string output = exec("ls -l");
std::cout << output;
return 0;
}
- 使用
fork()和exec()系列函数 这些函数提供了更细粒度的控制,允许你创建新的进程并替换它们的内存空间来运行新的程序。
#include
#include
#include
int main() {
pid_t pid = fork();
if (pid == 0) {
// 子进程
execl("/bin/ls", "ls", "-l", NULL);
// 如果execl返回,则表示失败
std::cerr << "Error executing ls\n";
return 1;
} else if (pid > 0) {
// 父进程
int status;
waitpid(pid, &status, 0); // 等待子进程结束
} else {
// fork失败
std::cerr << "Error creating child process\n";
return 1;
}
return 0;
}
- 使用第三方库 有一些第三方库提供了更高级的接口来执行外部命令和处理其输出,例如 Boost.Process。
#include
#include
#include
namespace bp = boost::process;
int main() {
std::string line;
bp::ipstream pipe_stream;
bp::child c("ls -l", bp::std_out > pipe_stream);
while (c.running() && std::getline(pipe_stream, line) && !line.empty())
std::cout << line << std::endl;
c.wait();
return 0;
}
在使用这些方法时,请确保考虑到安全性,特别是当执行的命令包含用户输入时,以避免注入攻击。此外,错误处理在这些示例中被简化了,实际应用中应该更加健壮。
以上就是关于“C++与Linux命令行交互技巧”的相关介绍,筋斗云是国内较早的云主机应用的服务商,拥有10余年行业经验,提供丰富的云服务器、租用服务器等相关产品服务。云服务器资源弹性伸缩,主机vCPU、内存性能强悍、超高I/O速度、故障秒级恢复;电子化备案,提交快速,专业团队7×24小时服务支持!
简单好用、高性价比云服务器租用链接:https://www.jindouyun.cn/product/cvm