Search code examples
c++binaryfiles

Seek n number of bits into a file (C++)


I have a file which contains data which is not byte aligned and I want to seek (e.g. 3 bits) into the file and then start reading chars into a buffer. Is there an "easy" way to do this?

I want to avoid bit-shifting each char in the buffer if possible.


Solution

  • Unless you're using a very interesting platform, your file contains bytes. And you read it one byte at a time. So there is no way to do it without bit shifting. The simplest way to hide the bit shifting I could think of, is to make an input iterator that stores the previously read byte and does the shift "behind the scenes".