Writing bit by bit onto a file
I am working on Huffman Encoding for compression.In order to reduce the size of files(effectively compress them) I need to replace each character by their coreesponding huffman code.So its highly likely that an 8 bit character by 2-3 bits.
I was wondering if anybody of you could help in writing bit by bit on a file.
Re: Writing bit by bit onto a file
Don't write bit-by-bit to a file, but fill a memory buffer bit-by-bit and once you have enough bits to fill at least a byte (or whatever size chunk makes sense; 256 bytes, 512 bytes, 1024 bytes, whatever), write the byte(s) to a file.
Of course, the last/final bits, if it is not an even byte, by chance, needs to be padded to full 8 bits. And you, of course need to know (store) what's such padding so that you don't mistakenly handle such padding as actual data/content that you need to process.