在这个 Boost.Process 0.5 ( http://www.highscore.de/boost/process0.5/index.html ) 的简单示例中,程序 ( ) 的输出ls
正在提供一个流。流工作正常,但与预期相反,程序完成后流不会变得无效(例如流结束)(类似于以前版本的 Boost.Process,例如http://www.highscore.de/boost /process/index.html )
is
为了使流(在示例中)在子程序退出后自动失效,我缺少什么?
也许这是我必须在 Boost.Streamsstream
中设置的选项file_descriptor
?
#include <boost/process.hpp> // version 0.5 from http://www.highscore.de/boost/process0.5/process.zip
#include <boost/iostreams/device/file_descriptor.hpp>
#include <boost/iostreams/stream.hpp>
#include <string>
using namespace boost::process;
using namespace boost::process::initializers;
using namespace boost::iostreams;
int main(){
boost::process::pipe p = create_pipe();
file_descriptor_sink sink(p.sink, close_handle);
child c = execute(run_exe("/usr/bin/ls"), bind_stdout(sink));
file_descriptor_source source(p.source, close_handle);
stream<file_descriptor_source> is(source);
std::string s;
while(std::getline(is, s)){
std::cout << "read: " << s << std::endl;
}
std::clog << "end" << std::endl; // never reach
}