gpt4 book ai didi

c++ - 如何让 boost::iostream 以类似于 std::ios::binary 的模式运行?

转载 作者:塔克拉玛干 更新时间:2023-11-03 02:20:11 24 4
gpt4 key购买 nike

我有以下关于 boost::iostreams 的问题。如果有人熟悉编写过滤器,我将非常感谢您的建议/帮助。

我正在编写一对多字符过滤器,它们与 boost::iostream::filtering_stream 一起用作数据压缩器和解压缩器。我从编写压缩器开始,从 lz 系列中学习了一些算法,现在正在研究解压缩器。

简而言之,我的压缩器将数据拆分为数据包,这些数据包分别编码,然后刷新到我的文件中。

当我必须从我的文件中恢复数据时(在编程术语中,接收一个 read(byte_count) 请求),我必须读取一个完整打包 block ,缓冲它,解压它,然后才给出请求的字节数。 我已经实现了这个逻辑,但现在我正在努力解决以下问题:


当我的数据被打包时,任何符号都可以出现在输出文件中。我在使用 boost::iostreams::read(...., size) 读取包含符号 (hex 1A, char 26) 的文件时遇到了麻烦。

例如,如果我使用 std::ifstream,我会设置一个 std::ios::binary 模式,然后可以简单地读取这个符号.

在实现使用 boost::iostream::read 例程读取字符序列的 boost::iostream 过滤器时,有什么方法可以达到同样的效果?


这里有一些代码:

   // Compression
// -----------
filtering_ostream out;
out.push(my_compressor());
out.push(file_sink("file.out"));

// Compress the 'file.in' to 'file.out'
std::ifstream stream("file.in");
out << stream.rdbuf();


// Decompression
// -------------
filtering_istream in;
in.push(my_decompressor());
in.push(file_source("file.out"));

std::string res;
while (in) {
std::string t;

// My decompressor wants to retrieve the full block from input (say, 4096 bytes)
// but instead retrieves 150 bytes because meets '1A' char in the char sequence

// That obviously happens because file should be read as a binary one, but
// how do I state that?
std::getline(in, t); // <--------- The error happens here

res += t;
}

最佳答案

以二进制形式读取文件的简答:

打开文件流时指定ios_base::binary

MSDN Link

关于c++ - 如何让 boost::iostream 以类似于 std::ios::binary 的模式运行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3129419/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com