[英]in boost iostream filtering_ostream, what is the difference between sync(), strict_sync() and flush()?
[英]Why boost::iostream::filtering_ostream using boost::iostreams::zlib_compressor needs to be destroyed for the sink to be written?
在花了很长时间调试今天的问题之后,我注意到需要销毁boost::iostream::filtering_ostream
才能写入接收器。
测试代码:
#include <boost/iostreams/filtering_stream.hpp>
#include <boost/iostreams/filter/zlib.hpp>
#include <sstream>
struct ZlibOstream : boost::iostreams::filtering_ostream
{
ZlibOstream(std::ostream& os)
{
boost::iostreams::filtering_ostream::push(boost::iostreams::zlib_compressor{});
boost::iostreams::filtering_ostream::push(os);
}
};
int main()
{
std::ostringstream oss;
#ifdef HAS_SCOPE
{
#endif
ZlibOstream zlibOstream{oss};
zlibOstream << "This is a test string.\n";
#ifdef HAS_SCOPE
}
#endif
return (oss.tellp() == 0);
}
调用flush()
不能解决问题,当我删除zlib_compressor
时我zlib_compressor
。
结果与coliru: https ://coliru.stacked-crooked.com/a/7cd166d2d820e838
这种行为背后的原因是什么?
这实际上与这个问题有关:
刷新boost :: iostreams :: zlib_compressor。 如何获得“同步刷新”?
你需要调用boost::iostreams::zlib_compressor::close
来进行刷新。
你可以通过在boost::iostream::filtering_ostream
上调用pop()
或reset()
来实现这一点。
注意, pop()
作为其名称建议弹出链中的最后一个过滤器并且reset()
完全清除链,以便之后不能使用filtering_ostream
。
例:
#include <boost/iostreams/filtering_stream.hpp>
#include <boost/iostreams/filter/zlib.hpp>
#include <sstream>
struct ZlibOstream : boost::iostreams::filtering_ostream
{
ZlibOstream(std::ostream& os)
{
boost::iostreams::filtering_ostream::push(boost::iostreams::zlib_compressor{});
boost::iostreams::filtering_ostream::push(os);
}
};
int main()
{
std::ostringstream oss;
ZlibOstream zlibOstream{oss};
zlibOstream << "This is a test string.\n";
zlibOstream.reset(); // needed if you want to write to oss
return oss.tellp();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.