I'm working on a project to compress/decompress a text file with the Huffman algorithm. I've successfully managed to compress a given text file into binary -- I've checked with the Visual Studio Hex Editor extension and it works correctly.
However, I don't really know how to go about implementing decompression. What I'd like to do is read the binary file bit-by-bit, traverse the corresponding Huffman tree, and write a character when a leaf is reached. However, I'm not sure how to go about reading a binary file bit-by-bit (I know that it can't be done directly as is: of course I had to do bit manipulation to implement compression in the first place).
I've tried using the fread() function to store the bits into a buffer, but frankly, I think that I'm misunderstanding how it works, and I'm not sure how much memory to give the buffer, nor how to retrieve the bits from the buffer, which is a char array.
Thanks in advance for any help.
Here is a simple read wrapper using an
intbuffer. It reads bits from the stream one at a time, starting from the least significant bit of the first byte:This function tries to minimize the number of memory reads, writes and the number of tests.
Here is an alternative that uses a structure to encapsulate the bitstream with an array to bufferize the file contents.