Reading values in order in file and assign.

My game is a sort of RPG with stats, money etc. I just recently added a save/load system using writing to a file. It was kinda over my head and skill level but I managed to get bits of code here and there to get it to work.

Anyway. I initially got writing to a text file to work, then I got loading to work too.I eventually was able to read numbers from the file and assign them to integer variables in order.

My issue was I wanted to check if a save file existed, if it did, load it up, if it did not, go to character creation. I had a lot of trouble with this and after trying different code snippets to work I finally got it to check if a file existed, and execute the appropriate code.

My issue now is my code USED to go through each entry and assign variables in order.

Like the first number in the text file would be for the variable money, and it would read it, assign to to int money and scroll to down to the next variable for player strength, assign to to playerstr variable and so on. After making the tweak for loading it no longer functions like this, and makes the last entry in the text file the value for everything.

Can anyone help me out here? I've very new, so explain like I'm five. :)

Here's my code:

Save Code:


1
2
3
4
5
6
7
8
9
{
ofstream savegame;
savegame.open("C:/Sounds/savegame.dat", ios::trunc);
savegame << money << endl;
savegame << playerstr <<endl;
savegame.close();
cout << "Saved game./n";
goto mainmenu;
}



Load Code:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
ifstream savegame("c:/sounds/savegame.dat"); // initialize savegame.dat file
		 std::string data; // intialize data integer

		//load savegame 

if (std::ifstream("c:/sounds/savegame.dat"))
{
	 
    
   while(! savegame.eof()) {
           getline(savegame, data);
			stringstream(data) >> money;
			stringstream(data) >> playerstr;
           std::cout << data << "\n\n";
   }

     std::cout << "File Loaded" << std::endl;
     goto mainmenu;
}


I barely get how this code works, how can I tweak it to go through the file in order and assign variables one at a time?

At the current moment, it assigned the playerstr value to both money and playerstr int. But the save file being created lists the correct values in order.
Last edited on
I noticed my program is outputting the variables in order, so it is cycling through them. How do I get it to stop at each entry, assign it a variable and move on? Why is the last number in the file being assigned to every variable?
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
#include <iostream>
#include <fstream>
#include <string>

int main()
{
    const char* const path = "C:/Sounds/savegame.dat" ;

    double money = 78.43 ;
    std::string playerstr = "some string, may be containing spaces" ;

    // save game
    {
        std::ofstream file(path) ; // open/create for output, truncate if existing
        file << money << '\n' << playerstr << '\n' ;
    }

    // load game
    {
        std::ifstream file(path) ; // open existing file for input

        if( file ) // file exists, opened successfully
        {
            file >> money ;

            // http://www.cplusplus.com/reference/istream/istream/ignore/
            file.ignore( 1000, '\n') ; // skip over the newline

            std::getline( file, playerstr ) ;
        }

        if( file ) // file is not in a failed state
        {
            std::cout << "data was read successfully\n"
                      << "money: " << money << '\n'
                      << "playerstr: " << playerstr << '\n' ;
        }
        else
        {
            std::cout << "failed to open file / read data from file\n" ;
        }
    }
}
I really appreciate the help. I see how it works much better when my code/variables are being used.

I actually did get a working system (maybe not the best).

By using this:


1
2
3
4
5
6
7
if (std::ifstream("c:/sounds/savegame.dat"))
{
getline(savegame, data) >> money;
getline(savegame, data) >> playerstr;
getline(savegame, data) >> playerint;
getline(savegame, data) >> playerluck;
}


It was giving me a problem by skipping the first entry in the list. But by adding a buffer value and adding it to the beginning of the save file. It loads all the values in order to the correct variables.

I will try implementing your method though, and I highly appreciate the help. Thank you.
Last edited on
If the string does not contain spaces, we don't need getline() at all.

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
#include <iostream>
#include <fstream>
#include <string>

int main()
{
    const char* const path = "C:/Sounds/savegame.dat" ;

    double money = 78.43 ;
    std::string playerstr = "some_string_without_spaces" ;
    int playerint = 89 ;
    int playerluck = 72 ;

    // save game
    {
        std::ofstream file(path) ; // open/creatre for output, truncate if existing
        file << money << '\n' << playerstr << '\n'
             << playerint << '\n' << playerluck << '\n' ;
    }

    // load game
    {
        std::ifstream file(path) ; // open existing file for input

        if( file >> money >> playerstr >> playerint >> playerluck )
        {
            std::cout << "data was read successfully\n"
                      << "money: " << money << '\n'
                      << "playerstr: " << playerstr << '\n'
                      << "playerint: " << playerint << '\n'
                      << "playerluck: " << playerluck << '\n' ;
        }
        else
        {
            std::cout << "failed to open file / read data from file\n" ;
        }
    }
}
Didn't know that, like I said, I was just finding examples online and trying to implement. Wasn't too sure what I was doing. But I;ve got it working with over 50 variables now. So thank you.

I am looking to do something else now.

When the player dies I want the save file to be deleted, how can I do this?
Topic archived. No new replies allowed.