I save struct array as binary. (fIn.write)
and I read it using below code.
std::ifstream fIn(LOG_PATH, std::ios::in|std::ios::binary);
...
IAttackSave_t IAttackSave;
while(fIn.read((char*)&IAttackSave, sizeof(IAttackSave_t)))
{
for(uint32 ulIdx = 0; ulIdx < ulCurLogCnt; ulIdx)
{
LIB_memcpy(Arr_IAttackSave[ulIdx], &IAttackSave, sizeof(IAttackSave_t));
}
}
But, (array) All of element in 'Arr_IAttackSave' has same struct !!!!
What's wrong with my code??
Thanks ahead.
CodePudding user response:
The outer loop reads elements one by one. The inner loop overwrites all elements of the array with the same element. After both loops finish, all elements have been overwritten with the element that was read last.
Instead, you need something like this:
for(uint32 ulIdx = 0; ulIdx < ulCurLogCnt; ulIdx)
{
if (!fIn.read((char*)&IAttackSave, sizeof(IAttackSave_t))) {
break;
}
LIB_memcpy(Arr_IAttackSave[ulIdx], &IAttackSave, sizeof(IAttackSave_t));
}