c++ - 如何使用 boost.process 重定向标准输入和标准输出

标签 c++ windows boost stdio boost-process

我正在尝试重定向子进程的标准输入和标准输出。 想用缓冲区中的二进制数据填充进程的标准输入并读取它,(但现在我只需要知道有多少写入标准输出)

namespace  bp = boost::process;
bp::opstream in;
bp::ipstream out;

bp::child c(Cmd.c_str(), bp::std_out > out, bp::std_in < in);

in.write((char*)buffer,bufferSize);
integer_type totalRead = 0;
char a[10240];
while (out.read(a,10240))  totalRead += out.gcount();
c.terminate();

写入看起来是成功的,但是程序卡在了 reading-while 循环中, 进程(子进程和父进程)在此期间保持空闲状态

最佳答案

工作代码,看起来我必须关闭内部管道来设置 child 的标准输入 eof( child 读取标准输入直到 eof(在我的例子中)):

namespace  bp = boost::process;
bp::opstream in;
bp::ipstream out;

bp::child c(Cmd.c_str(), bp::std_out > out, bp::std_in < in);    
in.write((char*)buffer,bufferSize);

in.pipe().close();

integer_type totalRead = 0;
char a[10240];
while (out.read(a,10240))  totalRead += out.gcount();
c.terminate();

关于c++ - 如何使用 boost.process 重定向标准输入和标准输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48657409/

相关文章:

c++ - 有没有人在 Visual Studio 2010 中使用 C++ 中的 JSON?

windows - 在 Windows 上使用 PHP 通过中继发送邮件

c++ - 是否可以在循环中多次调用 boost :asio io_service. run()?

c++ - 如何禁用有关某些库的编译器警告?

c++ - 为什么我的 C++ 代码中没有双重错误?

c++ - 返回 Eigen::Ref 是否合法?

c - 什么时候应该使用 Win32/WinAPI 类型与标准 C 类型?

c++ - 使用带有自定义图形的 r_c_shortest 路径( boost )

c++ - 为什么不能使用 '( )' 为类的非静态数据成员赋予默认值?

php - 使用 PHP 在 Windows 上更改文件创建日期