I'm working on a Huffman coding/decoding project in C and have a good understanding of how the algorithm should store information about the Huffman tree, re-build the tree during decoding, and decompress to the original input file using variable-length codes.
When writing to my compressed file, I will output a table of 256 4-byte integers containing unique frequencies, and I know I will also have to figure out a way to handle EOF - worrying about that later.
My question is how should I complete the necessary bit-wise operations to write a stream of variable-length codes to a series of 1-byte iterations of fwrite.
If I've created the following (fictitious) codes:
a: 001010101010011
b: 100
c: 11111
d: 0
The bitstream for "abcd" would be:
001010101010011100111110
I know I'll need to use some bit-wise operations to "chop" this stream up into writeable bytes:
00101010|10100111|00111110
A first attempt at creating 8 different cases based upon lengths of the codes did not work out well and I'm stumped. Is there an easier way to handle variable-length codes when writing to a file?
Thank you