I have a class for reading .ASE files and need to store the variables in a binary file to have faster access the next time the application runs. I store my information in a struct to make the writting process easier. This is the struct I use, defined in the header:
struct myMesh{
std::vector<Vector3> vertices;
std::vector<Vector2> uvs;
std::vector<Vector3> normals;
int verticesSize;
int uvsSize;
int normalsSize;
};
I have created an instance of this struct also in the header to define the variables:
myMesh myInfo;
After storing the data in the struct variables, I write a binary file using C functions:
std::string path = filename + ".bin";
const char * c = path.c_str();
FILE *pBinaryFile;
pBinaryFile = fopen(c, "wb");
if (pBinaryFile==NULL){
std::cout << "error";
}
fwrite(&myInfo.vertices, sizeof(myInfo), 1, pBinaryFile);
fclose(pBinaryFile);
To test if the binary is correctly created, I read the file and create another instance of the struct to visualize the data:
myMesh _myInfo;
FILE *theFile;
theFile = fopen(c, "rb");
if (theFile==NULL){
std::cout << "error";
}
fread(&_myInfo, sizeof(_myInfo), 1, theFile);
fclose(theFile);
And this works fine. The problem appears when I only try to read the file, just not using the the writting process:
/*FILE *pBinaryFile;
pBinaryFile = fopen(c, "wb");
if (pBinaryFile==NULL){
std::cout << "error";
}
fwrite(&myInfo.vertices, sizeof(myInfo), 1, pBinaryFile);
fclose(pBinaryFile);*/
myMesh _myInfo;
FILE *theFile;
theFile = fopen(c, "rb");
if (theFile==NULL){
std::cout << "error";
}
fread(&_myInfo, sizeof(_myInfo), 1, theFile);
fclose(theFile);
And now it does not work. The int variables of the struct are correctly acquired, but the vector variables appear in the form ??? with memory errors. I'm quite new with C++ and it's probably a silly question, but I don't get the point. I have also tried the C++ functions of ofstream and ifstream and I get the same issue.
Thanks in advance.