linux

C++与Linux命令行交互技巧

小樊
49
2025-09-05 05:32:24
栏目: 编程语言

在C++中与Linux命令行进行交互,通常涉及到执行外部命令并处理其输出。这可以通过多种方式实现,包括使用标准库函数、系统调用或者第三方库。以下是一些常用的方法和技巧:

  1. system() 函数 system() 是一个简单的调用,可以用来执行shell命令。它的原型在 <cstdlib> 头文件中定义。
#include <cstdlib>

int main() {
    int ret = system("ls -l");
    return ret;
}

注意:system() 函数会阻塞直到命令执行完毕,并且它不能很好地处理命令的输出。

  1. popen()pclose() 函数 popen() 函数可以打开一个指向命令的管道,并允许你读取命令的输出或向其写入数据。pclose() 用于关闭这个管道。
#include <cstdio>
#include <memory>
#include <stdexcept>
#include <string>

std::string exec(const char* cmd) {
    std::array<char, 128> buffer;
    std::string result;
    std::shared_ptr<FILE> 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;
}
  1. 使用 fork()exec() 系列函数 这些函数提供了更细粒度的控制,允许你创建新的进程并替换它们的内存空间来运行新的程序。
#include <unistd.h>
#include <sys/wait.h>
#include <iostream>

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;
}
  1. 使用第三方库 有一些第三方库提供了更高级的接口来执行外部命令和处理其输出,例如 Boost.Process。
#include <boost/process.hpp>
#include <iostream>
#include <string>

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;
}

在使用这些方法时,请确保考虑到安全性,特别是当执行的命令包含用户输入时,以避免注入攻击。此外,错误处理在这些示例中被简化了,实际应用中应该更加健壮。

0
看了该问题的人还看了