Save data

How can you make a C++ program save data from where it is and what variables are what?
WHAT? I'm sorry, could you explain it more precisly please? I don't understand anything you said!
I am making a C++ Command window Text Adventure, and I want it to save what number variables are currently set at. So if the player saves when they have an enchantment level of 5, the next time they go on the program and load a .dat or a save file, their enchantment level stays at five, and they will be at the same location they were when they saved.
Simply use file stream! First determine in which order are the saved variables, and then if the user wishes to save his/her progress, save the data in that order, and the next time the application is launched, prompt the user does he/she want to load an already saved file, and if so, load the data from the file in the corresponding variables.
You can use std::ofstream to write data to a file and std::ifstream to read the data back a file.
How do I do this? I've looked it up, but found nothing useful...
Okay, I understand how to create a file and edit it, but what can I do to load it?
Bump
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
//your includes...
//your usings
//your functions
int main()
{
      /*your variables that the player changes automaticaly
      during the course of the game*/
      cout<<"Load a saved file or start a new game?(L/N)\n";
      char choice;
      cin>>choice;
      if (choice=='L')
      {
             string file;
             cout<<"Enter the name of your file: \n";
             cin>>file;
             ifstream fin((file+".save").c_str(), ios::binary);
             //input your variables
             }
      //play the game
      cout<<"You chose to exit. Do you wish to save your current progress\n\
      to continue the game later?(Y/N)";
      cin>>choice;
      if (choice=='Y')
      {
             string save;
             cout<<"Enter the name of the file in which you want to save your progress: \n";
             cin>>save;
             ofstream fout((save+".save").c_str(), ios::binary);
             //output all the variables that determine a player's progress in the file.
             }
      //end the game
      }
Last edited on
Thanks, but how can I make it automatically update the variables after loading the file?
No, you have to enter them through the stream! Well, in theory you could, but that's too complicated! Just fin>> them at the beginning, and fout<< them at the end.
Topic archived. No new replies allowed.