I am trying to read chunks of data from a file directly into a struct but the padding is causing too much data to be read and the data to be misaligned.
Do I have to manually read each part into the struct or is there an easier way to do this?
My code:
The structs
typedef unsigned char byte; struct Header { char ID[10]; int version; }; struct Vertex //cannot rearrange the order of the members { byte flags; float vertex[3]; char bone; byte referenceCount; }; How I am reading in the data:
std::ifstream in(path.c_str(), std::ifstream::in | std::ifstream::binary); Header header; in.read((char*)&header.ID, sizeof(header.ID)); header.ID[9] = '\0'; in.read((char*)&header.version, sizeof(header.version)); std::cout << header.ID << " " << header.version << "\n"; in.read((char*)&NumVertices, sizeof(NumVertices)); std::cout << NumVertices << "\n"; std::vector<Vertex> Vertices(NumVertices); for(std::vector<Vertex>::iterator it = Vertices.begin(); it != Vertices.end(); ++it) { Vertex& v = (*it); in.read((char*)&v.flags, sizeof(v.flags)); in.read((char*)&v.vertex, sizeof(v.vertex)); in.read((char*)&v.bone, sizeof(v.bone)); in.read((char*)&v.referenceCount, sizeof(v.referenceCount)); } I tried doing: in.read((char*)&Vertices[0], sizeof(Vertices[0]) * NumVertices); but this produces incorrect results because of what I believe to be the padding.
Also: at the moment I am using C-style casts, what would be the correct C++ cast to use in this scenario or is a C-style cast okay?