Hi all,
For practice I'm trying to write 3D mesh data to a binary format. To eventually also be able to read meshes from binary files (provided by the asset pipeline).
I understand how to write floats, vectors of floats, ints etc by using a write((char*)myVar, sizeof(type) to the file, for which I'm using std::ofstream.
My goal is to write a 3D vector or other type (struct) to the file and be able to read it back the same way. From what I understand, I need to create function that returns a char array combining all members of the struct.
So in short; how can I create this char array, in a "safe" way?
So far I've come up with this approach:
- calculate number of needed bytes/chars, i.e 12 bytes for a 3D vector with 3 floats
- char result[12]
- cast the 3 individual floats to char[4]'s and use strcpy to result for the 1st and strcat for the others
Is this the way to go or would you suggest other approaches?
question 2; How would I read/convert the char array back to the 3 floats when reading it from the binary file?
Any input is appreciated.