Question

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;
   }
Was it helpful?

Solution

Short answer for reading file as binary :

specify ios_base::binary when opening file stream.

MSDN Link

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top