Boost 如何在打印后立即检索程序输出?

Boost 如何在打印后立即检索程序输出?,boost,boost-asio,boost-process,Boost,Boost Asio,Boost Process,我有一个boost::process::child。关于如何在单个向量中获取其所有stdout或stderr,有许多示例,但在这种方法中,您可以一次捕获所有数据。但如何在子进程中打印行/字符后立即检索它们 文件在这里: 使用ipstream 最简单的方法是: #include <boost/process.hpp> #include <iostream> namespace bp = boost::process; int main() { std:

我有一个boost::process::child。关于如何在单个向量中获取其所有stdout或stderr,有许多示例,但在这种方法中,您可以一次捕获所有数据。但如何在子进程中打印行/字符后立即检索它们

文件在这里:

使用
ipstream
最简单的方法是:

#include <boost/process.hpp>
#include <iostream>

namespace bp = boost::process;

int main() {
    std::vector<std::string> args { 
        "-c", 
        R"--(for a in one two three four; do sleep "$(($RANDOM%2)).$(($RANDOM%10))"; echo "line $a"; done)--" };

    bp::ipstream output;
    bp::child p("/bin/bash", args, bp::std_out > output);

    std::string line;
    while (std::getline(output, line)) {
        std::cout << "Received: '" << line << "'" << std::endl;
    }
}
使用
async\u管道
这种方法更加通用,允许您处理可能发生死锁的“硬”情况,例如,您希望同时执行其他操作,而不是阻塞输入

#include <boost/process.hpp>
#include <boost/process/async.hpp>
#include <boost/asio.hpp>
#include <iostream>

namespace bp = boost::process;
using boost::asio::mutable_buffer;

void read_loop(bp::async_pipe& p, mutable_buffer buf) {
    p.async_read_some(buf, [&p,buf](std::error_code ec, size_t n) {
        std::cout << "Received " << n << " bytes (" << ec.message() << "): '";
        std::cout.write(boost::asio::buffer_cast<char const*>(buf), n) << "'" << std::endl;
        if (!ec) read_loop(p, buf);
    });
}

int main() {
    boost::asio::io_service svc;

    std::vector<std::string> args { 
        "-c", 
        R"--(for a in one two three four; do sleep "$(($RANDOM%2)).$(($RANDOM%10))"; echo "line $a"; done)--" };

    bp::async_pipe output(svc);
    bp::child p("/bin/bash", args, bp::std_out > output, svc);

    char buf[1024];
    read_loop(output, bp::buffer(buf));

    svc.run();
}
#包括
#包括
#包括
#包括
名称空间bp=boost::process;
使用boost::asio::可变_缓冲区;
无效读取循环(bp::异步管道和p,可变缓冲区buf){
p、 异步读取(buf,[&p,buf](标准::错误代码ec,大小){

std::谢谢你的回答。顺便问一下,你用的是什么桌面?@LPCWSTR是tmux+powerline(我主要使用i3wm作为windowmanager,但它不可见)
#include <boost/process.hpp>
#include <boost/process/async.hpp>
#include <boost/asio.hpp>
#include <iostream>

namespace bp = boost::process;
using boost::asio::mutable_buffer;

void read_loop(bp::async_pipe& p, mutable_buffer buf) {
    p.async_read_some(buf, [&p,buf](std::error_code ec, size_t n) {
        std::cout << "Received " << n << " bytes (" << ec.message() << "): '";
        std::cout.write(boost::asio::buffer_cast<char const*>(buf), n) << "'" << std::endl;
        if (!ec) read_loop(p, buf);
    });
}

int main() {
    boost::asio::io_service svc;

    std::vector<std::string> args { 
        "-c", 
        R"--(for a in one two three four; do sleep "$(($RANDOM%2)).$(($RANDOM%10))"; echo "line $a"; done)--" };

    bp::async_pipe output(svc);
    bp::child p("/bin/bash", args, bp::std_out > output, svc);

    char buf[1024];
    read_loop(output, bp::buffer(buf));

    svc.run();
}