I have a vector:
class Element
{
public:
string pathName;
ui64 offsitePtr;
ui64 subPage;
public:
Element(void);
~Element(void);
};
vector<Element> elem;
The size of elem will be controlled less than 4096 bytes. At the end of the program, I should fwrite elem into a binary file. The solution I’m using currently is to make a char buffer and write the element in elem in it. I don’t think it is a good idea. Is there any other good ideas?
Provided you don’t write the vector or the
Elements directly from memory, it’s okay. You’ll need to serialize anything that isn’t POD (plain old data). That is in your case:vectorandstring.The vector is easy, because you can just make a function for
Element. But you probably want to serialize the vector size:For your element:
And you do something similar when you read. Will leave you to work that out =) Note in each case, you’re writing a size, and then the data. When you read, you read in the size, then resize your structure before reading the contents into it.
Oh, and make sure you open the file stream in binary mode.