This should be relatively easy to do, but after several hours straight programming my mind seems a bit frazzled and could do with some help.
I have a C++ class which I am currently using to store read/write data to file. I was initially using binary data, but have decided to store the data as CSV in order to let programs written in other languages be able to load the data.
The C++ class looks a bit like this:
class BinaryData
{
public:
BinaryData();
void serialize(std::ostream& output) const;
void deserialize(std::istream& input);
private:
Header m_hdr;
std::vector<Row> m_rows;
};
I am simply rewriting the serialize/deserialize methods to write to a CSV file. I am not sure on the “best” way to store a header section and a “data” section in a “flat” CSV file though – any suggestions on the most sensible way to do this?
The most sensible way is don’t do it. Let an existing library do it for you. You’ll never be able to put the requisite amount of work into this class that you’ll need to maintain backwards compatibility and changing requirements.
Use an existing solution, like Google’s protocol buffers. They’re stored in binary – so you get the size benefit, and there are many language bindings available. In addition, you can get human readable field-based ascii representations trivially.