如何让 boost::iostream 以与 std::ios::binary 相当的模式运行?

发布于 2024-09-07 18:43:23 字数 1680 浏览 2 评论 0原文

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

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

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

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


当我的数据被打包时,任何符号都可以出现在输出文件中。我在使用 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;
   }

I have the following question on boost::iostreams. If someone is familiar with writing filters, I would actually appreciate your advices / help.

I am writing a pair of multichar filters, that work with boost::iostream::filtering_stream as data compressor and decompressor.
I started from writing a compressor, picked up some algorithm from lz-family and now am working on a decompressor.

In a couple of words, my compressor splits data into packets, which are encoded separately and then flushed to my file.

When I have to restore data from my file (in programming terms, receive a read(byte_count) request), I have to read a full packed block, bufferize it, unpack it and only then give the requested number of bytes. I've implemented this logic, but right now I'm struggling with the following problem:


When my data is packed, any symbols can appear in the output file. And I have troubles when reading file, which contains symbol (hex 1A, char 26) using boost::iostreams::read(...., size).

If I was using std::ifstream, for example, I would have set a std::ios::binary mode and then this symbol could be read simply.

Any way to achieve the same when implementing a boost::iostream filter which uses boost::iostream::read routine to read char sequence?


Some code here:

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

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

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

发布评论

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

评论(1

黒涩兲箜 2024-09-14 18:43:23

将文件读取为二进制的简短答案:

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

MSDN 链接

Short answer for reading file as binary :

specify ios_base::binary when opening file stream.

MSDN Link

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