Load files

Ok, I have a load and save system set up for a small console based game I'm making to learn more about c++. So far, I have figured out how to write to a file and then load all the correct integers, but I can't re-assign those loaded integers to the program. If thats confusing, look at this code,

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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
                                        if (ans == 3){
                                              system("cls");
                                              cout<<"----------------------------------------"<<endl;
                                              cout<<"Saving your data"<<endl;
                                              cout<<"----------------------------------------"<<endl;
                                              //Creates an instance of ofstream, and opens example.txt
                                              ofstream a_file ( "pop.txt" );
                                              // Outputs to pop.txt through a_file
                                              
                                              if (a_file.is_open()){
                                              
                                              a_file<<"SAVED INFORMATION"<<endl;
                                              a_file<<"Male: "<<male<<endl;
                                              a_file<<"Female: "<<female<<endl;
                                              a_file<<"Total: "<<total<<endl;
                                              a_file<<"Day: "<<day<<endl;
                                              // Close the file stream explicitly
                                              a_file.close();
                                             
                                              system("pause");
                                              return 0;
                                              //yay it works! save and quit
                                              
                                              }
                                              
                                              else{
                                                   cout<<"Save unseccussful"<<endl;
                                                   }
                                              }
                                              
                                      if (ans == 4){
                                              //load *doesnt work*
                                             system("cls");
                                             cout<<"----------------------------------------"<<endl;
                                             cout<<"Loading saved data"<<endl;
                                             cout<<"----------------------------------------"<<endl;
                                             ifstream a_file ("pop.txt" );
                                             
                                             if (a_file.is_open()){
                                             
                                             a_file >> male;
                                             male = male;
                                             a_file >> female;
                                             female = female;
                                             a_file >> total;
                                             total = total;
                                             a_file >> day;
                                             day = day;
                                             cout<<"Load successful!"<<endl;
                                             system("pause");
                                             
                                             }
                                             
                                             else{
                                                  cout<<"Load unseccussful"<<endl;
                                                  system("pause");
                                                  }
                                              }


I open the file, the program finds the correct line and loads the number, but it won't assign the number to the program. And yes, I know system is bad to use.
Thanks for helping!
Last edited on
The file you make seems to be something like:

SAVED INFORMATION:
Male: 2
Female: 3
Total: 5
Day: 24


However, this:
1
2
3
4
5
6
7
8
a_file >> male;
male = male;  //Not needed
a_file >> female;
female = female; //Not needed
a_file >> total;
total = total; //Not needed
a_file >> day;
day = day; //Not needed 


Doesn't extract this properly I believe. You try to extract an integer from "SAVED INFORMATION:" and this aught to break the ifstream object. After extracting male, put the code:
if (!a_file.good()) cout << "File is broken";
This will tell you if the load actually loaded something.

Seems like this would be more appropriate for your file:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
string junk;
ifstream a_file("pop.txt");

//This

getline(a_file, junk);
a_file >> junk;
a_file >> male;
a_file >> junk;
a_file >> female;
a_file >> junk;
a_file >> total;
a_file >> junk;
a_file >> day;

//Or

getline(a_file, junk);
a_file >> junk >> male >> junk >> female >> junk 
       >> total >> junk >> total >> junk >> day;

Last edited on
OK thank you so much! I'll try that later(:
Topic archived. No new replies allowed.