对于我的一个项目,我正在编写一个保存文件阅读器。在最新版本中,我收到运行时错误Unhandled exception at 0x00075256 in Final Assessment.exe: 0xC0000005: Access violation writing location 0xCCCCCCCC.
我怎样才能继续识别和纠正这个错误的来源?
我相信当我创建 ifstream 对象时会发生错误saveFile()
,但我一直无法找到确切的来源。
bool Game::load(){
string data[9];
ifstream saveFile("save.dat");
int x = 0;
if (saveFile.is_open()){
while (!saveFile.eof()){
getline(saveFile,data[x]);
x++;
}
saveFile.close();
string savePlayerName = data[0];
int saveBaseHealth = stringToInt(data[1]);
int saveHealth = stringToInt(data[2]);
int saveKilled = stringToInt(data[3]);
int saveMoney = stringToInt(data[4]);
string saveWeaponName = data[5];;
int saveAttack = stringToInt(data[6]);
string saveArmorName = data[7];
int saveDefense = stringToInt(data[8]);
mainPlayer.setName(savePlayerName);
mainPlayer.setBaseHealth(saveBaseHealth);
mainPlayer.setHealth(saveHealth);
mainPlayer.setEnemiesKilled(saveKilled);
mainPlayer.setMoney(saveMoney);
mainWeapon.setName(saveWeaponName);
mainWeapon.setAttackPower(saveAttack);
mainArmor.setName(saveArmorName);
mainArmor.setDefense(saveDefense);
return true;
}
else
{
return false;
}
}