Boost C++ Libraries: Ticket #12945: boost::iostreams filtering streambufs only work with binary archive https://svn.boost.org/trac10/ticket/12945 <p> Since there is no portable binary format, using the text archive and gzipping it seems convenient. However, using boost::iostream's filtering_streambuf only works with the binary archive, not the text archive, which is somewhat surprising. </p> <div class="wiki-code"><div class="code"><pre><span class="cp">#include</span> <span class="cpf">&lt;iostream&gt;</span><span class="cp"></span> <span class="cp">#include</span> <span class="cpf">&lt;sstream&gt;</span><span class="cp"></span> <span class="cp">#include</span> <span class="cpf">&lt;boost/archive/binary_oarchive.hpp&gt;</span><span class="cp"></span> <span class="cp">#include</span> <span class="cpf">&lt;boost/archive/text_oarchive.hpp&gt;</span><span class="cp"></span> <span class="cp">#include</span> <span class="cpf">&lt;boost/iostreams/filter/gzip.hpp&gt;</span><span class="cp"></span> <span class="cp">#include</span> <span class="cpf">&lt;boost/iostreams/filtering_streambuf.hpp&gt;</span><span class="cp"></span> <span class="cp">#include</span> <span class="cpf">&lt;boost/serialization/vector.hpp&gt;</span><span class="cp"></span> <span class="kt">int</span> <span class="nf">main</span><span class="p">()</span> <span class="p">{</span> <span class="k">const</span> <span class="n">std</span><span class="o">::</span><span class="n">vector</span><span class="o">&lt;</span><span class="kt">int</span><span class="o">&gt;</span> <span class="n">v</span><span class="p">{</span><span class="mi">0</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">};</span> <span class="n">std</span><span class="o">::</span><span class="n">stringstream</span> <span class="n">ss</span><span class="p">;</span> <span class="p">{</span> <span class="n">boost</span><span class="o">::</span><span class="n">iostreams</span><span class="o">::</span><span class="n">filtering_ostreambuf</span> <span class="n">o</span><span class="p">;</span> <span class="n">o</span><span class="p">.</span><span class="n">push</span><span class="p">(</span><span class="n">boost</span><span class="o">::</span><span class="n">iostreams</span><span class="o">::</span><span class="n">gzip_decompressor</span><span class="p">());</span> <span class="n">o</span><span class="p">.</span><span class="n">push</span><span class="p">(</span><span class="n">ss</span><span class="p">);</span> <span class="c1">// filtering_ostreambuf isn&#39;t accepted by text_oarchive&#39;s ctor, only by binary_oarchive</span> <span class="c1">// So this doesn&#39;t work</span> <span class="n">boost</span><span class="o">::</span><span class="n">archive</span><span class="o">::</span><span class="n">text_oarchive</span> <span class="n">oa</span><span class="p">(</span><span class="n">o</span><span class="p">);</span> <span class="c1">// But this does</span> <span class="c1">// boost::archive::binary_oarchive oa(o);</span> <span class="n">oa</span> <span class="o">&lt;&lt;</span> <span class="n">v</span><span class="p">;</span> <span class="p">}</span> <span class="n">std</span><span class="o">::</span><span class="n">cout</span> <span class="o">&lt;&lt;</span> <span class="n">ss</span><span class="p">.</span><span class="n">str</span><span class="p">()</span> <span class="o">&lt;&lt;</span> <span class="sc">&#39;\n&#39;</span><span class="p">;</span> <span class="k">return</span> <span class="mi">0</span><span class="p">;</span> <span class="p">}</span> </pre></div></div> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/12945 Trac 1.4.3 Robert Ramey Wed, 03 May 2017 22:01:43 GMT <link>https://svn.boost.org/trac10/ticket/12945#comment:1 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/12945#comment:1</guid> <description> <p> Indeed - it IS surprising. </p> <p> And it's not toooooooo hard to fix. I'll look into it. </p> </description> <category>Ticket</category> </item> <item> <dc:creator>Robert Ramey</dc:creator> <pubDate>Wed, 03 May 2017 22:17:58 GMT</pubDate> <title>status changed; resolution set https://svn.boost.org/trac10/ticket/12945#comment:2 https://svn.boost.org/trac10/ticket/12945#comment:2 <ul> <li><strong>status</strong> <span class="trac-field-old">new</span> → <span class="trac-field-new">closed</span> </li> <li><strong>resolution</strong> → <span class="trac-field-new">invalid</span> </li> </ul> <p> for text files use filtering_ostream rather than filtering_ostreambuf . </p> <p> text files must use streams. binary files may use streams or streambuf </p> <pre class="wiki">#include &lt;iostream&gt; #include &lt;sstream&gt; #include &lt;boost/archive/binary_oarchive.hpp&gt; #include &lt;boost/archive/text_oarchive.hpp&gt; #include &lt;boost/iostreams/filter/gzip.hpp&gt; #include &lt;boost/iostreams/filtering_stream.hpp&gt; #include &lt;boost/serialization/vector.hpp&gt; int main() { const std::vector&lt;int&gt; v{0, 1, 2}; std::stringstream ss; { boost::iostreams::filtering_ostream o; o.push(boost::iostreams::gzip_decompressor()); o.push(ss); // filtering_ostreambuf isn't accepted by text_oarchive's ctor, only by binary_oarchive // So this doesn't work boost::archive::text_oarchive oa(o); // But this does // boost::archive::binary_oarchive oa(o); oa &lt;&lt; v; } std::cout &lt;&lt; ss.str() &lt;&lt; '\n'; return 0; } </pre> Ticket fiesh@… Thu, 04 May 2017 05:57:56 GMT <link>https://svn.boost.org/trac10/ticket/12945#comment:3 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/12945#comment:3</guid> <description> <p> Oh, I'm sorry! In my defense I'd like to add I had several people look at it and they didn't see it. I guess it's confusing text archives work with both while binary ones don't, but of course that's not a bug. </p> <p> Thank you! </p> </description> <category>Ticket</category> </item> </channel> </rss>