I have a set of .bin files containing data in a formally specified format. I know exactly how many bytes there are for each field e.g. name = 40 bytes, version number = 2 bytes etc. I also know the exact order they are stored in the file (e.g. name, then version number....).
So far I can load the data from a file into an std::vector<unsigned char>
list, then step through that data and read the fields in as per the number of expected bytes.
The issue is that this method is very long and error prone should I get any of the fields wrong (there's alot of different fields).
I've looked at and talked to people about struct packing, pointer casting and bit fields. I just can't seem to get them all to work together.
How can I read the data into my buffer, then 'overlay' my struct on the buffer? Then all the fields would populate according to the allocated bit fields I've given each value in the struct.
The issue with bit fields is that I can't take in strings.
Advice or example code would be highly appreciated. If you'd like just comment and I can give you code to show what I have so far and what I'm trying to achieve.
#include <vector>
int main()
{
//File data loaded by function call
std::vector<unsigned char> fileData;
//How do I cast fileData to be a dataFields type?
}
struct dataFields
{
int ID : 8;
// Cannot use bit field for string type?
std::string name;
int versionNumber : 16;
int someOtherValue : 8;
}
I cannot give the exact code I'm working on for work reasons but I feel this sumarises what I'm trying to do fairly well in a simple manor.