@
Catfish
Your example uses vocabulary that will confuse the OP -- your "code table" is not the same thing as a Huffman code table.
@
targt123
The smallest you can write to disk is eight bits at a time (or one byte). However, when dealing with compression, you must deal with individual bits.
What you must do is create a cache to store bits until you get enough (eight) to write.
[ _ _ _ _ _ _ _ _ ]
-- A cache with no bits used.
If I put a bit in, then the cache still has seven empty spaces.
[ _ _ _ _ _ _ _ 1 ]
-- A cache with one bit used.
If I put another bit in, it has fewer.
[ _ _ _ _ _ _ 0 1 ]
-- A cache with one bit used.
I keep adding bits:
[ _ 1 1 0 1 0 0 1 ]
-- A cache with all but one bit used.
When I add the final bit, the cache is full:
[ 1 1 1 0 1 0 0 1 ]
-- A full cache (with all bits used).
Now that the cache is full, I can write its value (in this example, 0xE9 or 233 decimal) to file, and reset my cache to empty.
[ _ _ _ _ _ _ _ _ ]
-- A cache with no bits used.
The simplest way to implement a cache is to keep track of how many bits are currently in it. A simple class to store the cache, the number of bits used, and methods to accept a bit to add to the cache and to flush should suffice. If you don't want to use a class, you can use functions with either global or static 'cache' and 'number of bits used' values.
-----
Likewise, when you are compressing values, you also need to know how many bits are used in a code (in addition to what those bit values are). For example, a simple Huffman table:
111 - three bits
010 - three bits
000 - three bits
1101 - four bits
1010 - four bits
...
I yoinked the table from here: http://en.wikipedia.org/wiki/Huffman_coding
To output the fourth code, I send them to the output cache, one bit at a time: 1, 1, 0, 1. If I next output the first code (1, 1, 1), and then the fifth code (1, 0, 1, 0), I am doing my job -- I know that the data is being written to disk whenever the cache gets full.
When you are done writing all bits, flush the cache to make sure that all bits are written to disk -- that is, if it is not empty, just write it (and all the extra, random bits it contains) to disk.
-----
Pay attention to the
order in which you put bits into the cache. Notice how I wrote them so that your decoder can read a bit and follow the bit pattern from the root of the tree down to a leaf. This is important.
That should be enough to get you started. Good luck!