#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;
}
Saving and Loading in a Text based game.
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?
Maybe? If that works how would I retrieve the data.
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;
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!
If you have any more particular questions, let 'em rip!
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement