【发布时间】:2010-06-28 00:10:49
【问题描述】:
我对@987654322@ 有以下问题。如果有人熟悉编写过滤器,我真的很感谢您的建议/帮助。
我正在编写一对多字符过滤器,它们与 boost::iostream::filtering_stream 一起用作数据压缩器和解压缩器。
我从写一个压缩器开始,从 lz-family 学习了一些算法,现在正在研究一个解压缩器。
简而言之,我的压缩器将数据拆分为数据包,这些数据包分别编码,然后刷新到我的文件中。
当我必须从我的文件中恢复数据时(用编程术语来说,接收read(byte_count) 请求),我必须读取一个 full 打包块,对其进行缓冲、解包,然后才给出请求的字节数。 我已经实现了这个逻辑,但现在我正在努力解决以下问题:
当我的数据被打包后,任何符号都可以出现在输出文件中。而且我在读取文件时遇到了麻烦,其中包含符号(hex 1A, char 26) 使用boost::iostreams::read(...., size)。
例如,如果我使用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;
}
【问题讨论】:
-
一个可编译的测试用例总能打败文字墙。
-
我遇到了类似的问题:stackoverflow.com/questions/224234/…
标签: c++ iostream eof boost-iostreams