BOOST.IOstreams:写入 bzip2 时遇到问题

发布于 2024-08-27 21:03:25 字数 1579 浏览 5 评论 0原文

您好,我想使用 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";
                }
        }
};

也许有人可以解释为什么?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

氛圍 2024-09-03 21:03:25

fstream 无法复制,因此您必须使用 Push 的参考版本

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) ; 

我很惊讶您编译器允许您的代码,我认为它会抱怨对临时...您使用哪个编译器?

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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文