2010-04-01 84 views
6

您好,我希望使用Boost.IOstream將我的數據存儲到bzip2文件中。BOOST.IOstreams:寫入bzip2的麻煩

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. 
} 
}; 

我在做什麼錯了? 我正在使用boost 1.42.0。

親切的問候 阿曼。

編輯 的代碼工作,如果我刪除的雙向選擇:

#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"; 
       } 
     } 
}; 

也許有些人能解釋爲什麼嗎?

+0

第二個代碼段也不能用gcc編譯。 my_filter.push(std :: fstream(...))'沒有匹配函數' – 2010-04-05 03:31:32

+0

@epronk:它是什麼編譯器?我使用gcc 4.4.4:g ++ bz_test.cpp -I $ {BOOSTROOT}/include -L $ {BOOSTROOT}/lib -lboost_iostreams,它編譯時沒有問題。我剛剛編輯了這篇文章。 – Arman 2010-04-05 18:21:59

+0

你解決了這個問題嗎?如果是這樣,請發佈答案。 – Cookie 2011-09-02 10:24:54

回答

3

fstream無法複製,所以你必須使用推的參考版本

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

所以你的函數應該是這個樣子

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

我驚訝,你的編譯器可以讓你的代碼,我認爲它會抱怨臨時引用......你正在使用哪種編譯器?

+0

@Pieter:我正在使用VC++ 2008 Express版本:版本9.0.30729.1 SP。代碼順利編譯,沒有任何警告。您的建議不能解決問題。和以前一樣,它停在:iostreams/src/bzip2.cpp函數中的void bzip2_base :: end(bool compress)。 – Arman 2010-04-01 09:35:17

+2

對我來說,你已經達到了你需要進入boost郵件列表的地步。 – 2010-04-07 12:19:10

+0

@Ben Collins:是的,那是我做的。謝謝。 – Arman 2010-04-08 10:52:18