I need to read in/write out vector of struct from/to csv file. The struct has over 80 field (which contain int/float and string). I know how to read in/write out the struct by using ofstream 1 field by 1 field. However its tedious and error prone. At first I try to find some method to map each field to an array
i.e. MyStruct.Field1 can be access by MyStruct.AllField[12]
so that when process the data, I can use MyStruct.Field1, MyStruct.Param1 etc which has a meaningful name and when read in/output the data, I can use a simple for loop.
However, implement such datastructure also tedious (not to mention my struct contain various data type).
Next, I try to look for some class to easy my life, I find that CSV++ and cxxtools may suit my need (can read in/output struct at once ofs << MyStruct), however, these 2 classes are designed for Linux and I am working on VC++!! The makefile is not compatible.
I would like to know is there any instruction on using these classes in VC++ or any other classes for VC++ can perform similar task ? Thanks.
Have you tried ditching the makefile and trying to compile it yourself?
If you *must* you may also be able to compile it into a usable library with a g++ on windows tool, like Cygwin?
I suspect some tedium is going to be a fact of life if you do this yourself :) its just a fat piece of data.
what about a table of the offsets? eg
int offsets[] = {12,16,32,87,whatever}. //if you want to name them, you can put them in an enum instead of hard-coded directly.
it may be tedious, but its simple and gets it done.
I am not really clear on what you really have, so that could become
struct offset
{
int value; some_enum type; //enum of int, float, string, etc for the type
}
and something like above with if statements
if(offset[z].type == stringtype)
serial << (char*)(mystruct.allfield[offsets[z].value] ….
if(… == inttype)
…
etc
this seems clunky, though.
wouldn't it be more straightforward to overload << for the struct to print in csv format directly? I think I would do it this way, if possible.