簡體   English   中英

ifstream struct read(我的代碼有什么問題??)

[英]ifstream struct read (What's wrong with my code??)

我將結構數組保存為二進制文件。 (fIn.write)

我使用下面的代碼閱讀了它。

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));
        }
}

但是,(數組)'Arr_IAttackSave' 中的所有元素都具有相同的結構!!!!

我的代碼有什么問題??

先謝謝了。

外循環一個一個地讀取元素。 內部循環用相同的元素覆蓋數組的所有元素。 兩個循環完成后,所有元素都被最后讀取的元素覆蓋。

相反,你需要這樣的東西:

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));
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM