BOOST.IOstreams: trouble to write to bzip2
Asked Answered
B

1

6

Hello I am would like to store my data in to bzip2 file using Boost.IOstreams.

void test_bzip()
{
namespace BI = boost::iostreams;
{
string fname="test.bz2";
  {
    BI::filtering_stream<BI::bidirectional> my_filter; 
    my_filter.push(BI::combine(BI::bzip2_decompressor(), BI::bzip2_compressor())) ; 
    my_filter.push(std::fstream(fname.c_str(), std::ios::binary|std::ios::out)) ; 
    my_filter << "test" ; 

    }//when my_filter is destroyed it is trowing an assertion.
}
};

What I am doing wrong? I am using boost 1.42.0.

kind regards Arman.

EDIT The code is working if I remove the bidirectional option:

#include <fstream> 
#include <iostream> 
#include <boost/iostreams/copy.hpp> 
#include <boost/iostreams/filter/bzip2.hpp> 
#include <boost/iostreams/device/file.hpp> 
#include <boost/iostreams/filtering_stream.hpp>
#include <string>



void test_bzip()
{
        namespace BI = boost::iostreams;
        {
                std::string fname="test.bz2";
                {
                        std::fstream myfile(fname.c_str(), std::ios::binary|std::ios::out); 
                        BI::filtering_stream<BI::output> my_filter; 
                        my_filter.push(BI::bzip2_compressor()) ; 
                        //my_filter.push(std::fstream(fname.c_str(), std::ios::binary|std::ios::out)) ; //this line will work on VC++ 2008 V9 but not in G++ 4.4.4
                        my_filter.push(myfile);
                        my_filter << "test";
                }
        }
};

maybe some one can explain why?

Bifurcate answered 1/4, 2010 at 8:59 Comment(4)
The second snippet also doesn't compile with gcc. my_filter.push(std::fstream(...)) 'no matching function'Maliamalice
@epronk: what compiler is it? I use gcc 4.4.4: g++ bz_test.cpp -I${BOOSTROOT}/include -L${BOOSTROOT}/lib -lboost_iostreams and it compiles without problem. I just edited the post.Bifurcate
Did you solve this? If so please post an answer.Bloodletting
@Cookie: Look at EDIT, thats works.Bifurcate
B
3

An fstream can not be copied, so you must use the reference version of push

template<typename StreamOrStreambuf>
void push( StreamOrStreambuf& t,
           std::streamsize buffer_size = default value,
           std::streamsize pback_size = default value );

So your function should look something like

std::fstream theFile(fname.c_str(), std::ios::binary | std::ios::out);
// [...]
my_filter.push(theFile) ; 

I'm suprised you compiler allows your code, I'd think it complain about a reference to temporary... which compiler are you using?

Bankroll answered 1/4, 2010 at 9:17 Comment(3)
@Pieter: I am using VC++ 2008 Express edition: Version 9.0.30729.1 SP. The code compiles smoothly without any warning. Your suggestion does not solve the problem. As before it is stops in: void bzip2_base::end(bool compress) in iostreams/src/bzip2.cpp function.Bifurcate
Seems to me you've reached the point at which you need to go to the boost mailing lists.Gnaw
@Ben Collins: Yes, that what I did.Thanks.Bifurcate

© 2022 - 2024 — McMap. All rights reserved.