Saving and Loading in a Text based game.

Started by
1 comment, last by jouley 15 years, 10 months ago
How would I go about saving and loading the players stats into some sort of file, and retrieve them at a later point if they wish to continue with that character. Something like this?

#include <iostream>
#include <fstream>
#include <string>

int main()
{
std::string PlayerName = "HEro";
std::ofstream Save("Player.txt", std::ios::app);
Save << PlayerName; << std::endl;
Save.flush();
Save.close();
return 0;
}

Maybe? If that works how would I retrieve the data.
Advertisement
That is fine.

Using ifstream (also part of the fstream header), you open the file just like with ofstream, and stream like if you were using std::cin.

Example:
save>>PlayerName;
The general term for this process is serialization, and much more informative information on it can be found here, along with google. The former should give you more than enough to look at for a while, even when you need to save fancy game states, like inventories, multiple characters, and the like.

If you have any more particular questions, let 'em rip!

This topic is closed to new replies.

Advertisement