Domanda

For my current task I need a possibility to read/write (mostly file based) bitstreams. Though this is a more or less trivial task if coded in standard C/C++ I'd like to rewrite to code using a more generic approach by overloading and using the standard STL iostream or similar so I can write something like

writeHeader();
{
    ofstream outfile ("test.bin");
    outfile << true; // Write 1 bit
    outfile << false; // Write 1 bit
    outfile << (char)0x42; // Write 8 bits
}

However I'm not sure which road to go:

  1. Using Boost.IOStream or
  2. Derive from streambuf or iostream directly.

So far I never had to derive/provide my own stream classes but I want to improve my knowledge...

Maybe someone can provide some pointers or hints which way to prefer and why!?

Thanks!

È stato utile?

Soluzione

Your best bet is probably to make both a new stream class and a stream buffer class. Use the custom stream class to overload the output operators to put a single bit in the custom stream buffer.

The reason is that C++ doesn't support smaller entities than a single byte (char). Even the bool type is not a single bit, as it has to be addressable.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top