basically, I’ve got my Huffman table as
std::map<std::string, char> ciMap;
Where string is the bit pattern and char is the value represented by said pattern. The problem is how do I store that as a header of my compressed file so I can build again the same map when I want to decode it?
Trying to store it as binary:
size_t mapLen = ciMap.size(); outFile.write(reinterpret_cast<char*>(&mapLen), sizeof(size_t)); outFile.write(reinterpret_cast<char*>(&ciMap), sizeof(ciMap));
And later building with:
inFile.read(reinterpret_cast<char*>(&mapLen), sizeof(size_t)); inFile.read(reinterpret_cast<char*>(&ciMap), sizeof(mapLen));
Doesn’t work, I get string initilization error… something to do with NULL. Any suggestions? If you have better way of storing the bits and values I’d like to hear.
You can do it yourself, or you can do it with boost: http://www.boost.org/doc/libs/1_37_0/libs/serialization/doc/index.html. What you currently try is just view the map as a plain old datatype, which essentially means it’s a C datatype. But it isn’t, so it fails to save/load. boost serialization does it correctly. Have a look at it. If you don’t want to use it, you can do something like this:
Note that the above needs some changes if the characters stored could be whitespace characters too. Because of that, it’s probably the best to first convert to an int before writing out, and then reading as an int when loading. Actually, i recommend boost serialization, and boost iostreams (http://www.boost.org/doc/libs/1_37_0/libs/iostreams/doc/index.html), which includes a compression stream that transparently can compress your data too.