4

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

我正在编写一对多字符过滤器,boost::iostream::filtering_stream可用作数据压缩器和解压缩器。 我从写一个压缩器开始,从 lz-family 学习了一些算法,现在正在研究一个解压缩器。

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

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


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

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

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


这里有一些代码:

   // 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;
   }
4

1 回答 1

1

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

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

MSDN 链接

于 2010-06-28T00:57:05.660 回答