I' putting together a program that compresses the frequencies of a Huffman code into binary but am unsure on how exactly to do this. I have an array of Nodes as defined:
We are supposed to output the frequencies as 4-byte unsigned ints. We have an example of a compressed file, and it is unreadable by humans, which obviously means that I'm not just looking for how to make a number like 5 into 101. We take information in through cin and are supposed to output it through cout. Thanks in advance!
Next, you need a way of properly packing and unpacking multi-byte values to and from file. You must choose whether or not you want your file to store things is Big-Endian or Little-Endian (or some other oddball Mixed-Endian) format.
#include <cstdint>
...
usingnamespace std;
usingnamespace big_endian_io; // If your file stores multi-byte words in big endian
int main()
{
std_binary_io(); // switch to binary I/O on the standard streams
...
uint32_t value; // size-specific types
read_word( cin, value );
value ~= value; // do something to value... (you'd actually want to compress or decompress the stream here...)
write_word( cout, value );
}
If the binary file is Huffman coded data then it's probably best to read the data section a single byte at a time, so endianness will not be a problem. Even the header section will be a byte (character) followed by it's encoding, which will also fit into a byte. So the whole thing may be readable a byte at a time.
What is the format of the file?
What do you mean by compressing the freqs into binary?
(BTW, the extra long line above can be split across lines like so:
> it's probably best to read the data section a single byte at a time
Agreed. I was going to comment on this, but the OP specifically stated that:
> We are supposed to output the frequencies as 4-byte unsigned ints
Knowing nothing more about the assignment than what he has told us, I suspect that his professor isn't too worried about bit-packing the data just yet... only in outputting the huffman tree...
I also always make sure that if I am programming on Windows that __WIN32__ is #defined... but I haven't any clue what compiler he's using and that line covers most of them...