So, I've gotten myself in a bit of a pickle. I have been serializing data I collect with:
vector<myClass> mVec;
... // mVec is filled with the data to collect
for (auto it = mVec.begin(); it != mVec.end(); ++it)
{
myfile.write(reinterpret_cast<char*>(&(*it)), sizeof(*it));
}
I've wrapped this code in a templated function, and used it a LOT over the past couple of years. However, over the past 3 weeks, I have made a huge mistake because the object I've been serializing is not always the same size:
class myClass
{
std::string m_str;
// ... other data
};
Because myClass contains a string, it's size can vary, so my usual ReadDATA() is not going to work:
template<class T>
static void ReadData(std::vector<T>* pData
, const char* pszFileName)
{
std::ifstream myfile;
T tp;
myfile.open(pszFileName, std::ios::binary);
while (myfile.read(reinterpret_cast<char*>(&tp)
, sizeof(tp)).gcount() == sizeof(tp))
{
pData->push_back(T(tp));
}
myfile.close();
}
That is the bad news. The good news, the m_str object of my class is a finite size (Let's say a max of 10 total characters).
Is there any way I can possibly recover this data of objects of unknown size? I am really hoping that I did not just lose 3 weeks of data over a silly mistake! I've been in permanent face-palm mode ever since I realized my error earlier today. I will be very, very thankful for any help!