How to make a basic save and load function?

Well, I have one heck of a problem here. I'm making this simple game (code of the game not included) where one manages a colony in space. I've planned to include a save feature for the game, but I have no proper idea on how to make one. I got this far while testing:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
 #include <fstream>
#include <iostream>

using namespace std;

struct savedata{
    int a;
    int b;
    int c;
};

int d;
int e;

int main()
{
  savedata save;
  savedata *ptr;
  ptr = &save;

  save.a = 1;
  save.b = 2;
  save.c = 3;

  d = 4;
  e = 5;

  cout<<"\n";
  ofstream a_file ("example.sav");
  a_file << ptr->a << "\n" << ptr->b << "\n" << ptr->c << "\n" << d << "\n" << e << "\n";
  a_file.close();

  //Then comes the main problem.
  //How do I load it and assign those values back to savedata's save's a, b, c, and
  //the integers d and e?
}


As I'm a complete beginner, I really need some help, and some clear ones too. I would really appreciate if one you guys explained a way to do it step by step.
And if the comment was unclear, I want to load every number/text I wrote in it to be loaded into my game. Any help, please?

*Note: I've spent hours trying to find a way...
Sounds like you might want to save data to a text file and then read the text file back in when the program starts again?

There's a bit on file input/output in the tutorial here.
http://www.cplusplus.com/doc/tutorial/files/
Topic archived. No new replies allowed.