Is it possible to do this?

closed account (38qG1hU5)
Is it possible to make a program create file with certain data points, and then load the file and read the data inside? (Sorry If this is a newb question i'm quite new to C++)

As example.
Timmy plays a game and scores 100 points. Timmy "Saves" and closes. 5 minutes later Timmy loads up the game and wants to resume his current save file.

I want to make a simple mess around project for an IT class I'm in.
Yes.
Lol @ helios

op: look at fstream.

For direct serialization of trivial objects it will do what you need. If you need a stronger method you can use a markup language- I like json myself.
Simple:

1
2
3
4
5
6
7
8
9
void SaveScore(int score, const std::string& save_loc) {
    std::ostream oss = std::ofstream(save_loc.c_str());
    oss << score << '\n';
}

void ReadScore(int &score, const std::string& save_loc) {
    std::istream iss = std::ifstream(save_loc.c_str());
    iss >> score;
}


As well as json, you should also look into proto buffers which I think will be better to use for this sort of thing
https://developers.google.com/protocol-buffers/?hl=en
Last edited on
That Google one looks quite cool, I assume it can handle nested classes, containers, etc?

Do you if it can handle pointers and create the pointed to object along with assigning a pointer in the main object to the correct location? (That would be quite impressive and extremely useful)
No, it doesn't handle graphs, but I'm actually working on a generator that does, including handling of reference cycles.
Topic archived. No new replies allowed.