【问题标题】:How to get boost::iostream to operate in a mode comparable to std::ios::binary?如何让 boost::iostream 在与 std::ios::binary 相当的模式下运行?
【发布时间】:2010-06-28 00:10:49
【问题描述】:

我对@9​​87654322@ 有以下问题。如果有人熟悉编写过滤器,我真的很感谢您的建议/帮助。

我正在编写一对多字符过滤器,它们与 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;
   }

【问题讨论】:

标签: c++ iostream eof boost-iostreams


【解决方案1】:

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

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

MSDN Link

【讨论】:

  • 看起来不错。但是我是否有任何选择,例如强制我的filter 仅在相应设备处于binary 模式时工作?还是这个不能查?
  • 在“二进制模式设备”的情况下您指的是什么?
猜你喜欢
  • 1970-01-01
  • 2011-01-19
  • 2014-05-19
  • 1970-01-01
  • 2017-07-02
  • 2011-09-15
  • 2014-11-08
  • 1970-01-01
相关资源
最近更新 更多