In my C++ program I am trying to develop a simple saving and loading function for my game. I can easily get the save function on it, but the loading part of it really stumps me.
What I need is to load the numbers on the file (line by line) and insert them into different variables for my player. I'm just not able to figure that out. I've even looked all around the inter webs.
Here is the code for the player save and load functions:
void Player::Save()
{
std::ofstream savePlayerFile;
savePlayerFile.open("Save/playerSave.dat", std::ofstream::trunc);
savePlayerFile << x << "\n" << y << "\n" << prevX << "\n" << prevY << "\n" << health << "\n" <<
health << "\n" << mana << "\n" << gold << "\n" << level << "\n" << maxHealth << "\n" <<
maxMana << "\n" << strength << "\n" << defense;
savePlayerFile.close();
}
void Player::Load()
{
std::ifstream loadPlayerFile;
loadPlayerFile.open("Save/playerSave.dat", std::ofstream::in);
/*std::string line;
while(std::getline(loadPlayerFile, line))
{
std::istringstream iss(line);
*/
loadPlayerFile >> ( x && "\n" && y && "\n" && prevX && "\n" && prevY && "\n" && health && "\n" &&
health && "\n" && mana && "\n" && gold && "\n" && level && "\n" && maxHealth && "\n" &&
maxMana && "\n" && strength && "\n" && defense);
}
\n
for writing, use spaces (" "), and in reading, remove the(
/)
, replace&&
by >>, and remove all"\n"
:loadPlayerFile >> x >> y >> prevX >> prevY >> [...];
\$\endgroup\$