Saving Files

closed account (1v5E3TCk)
I am working on a text-based RPG game and I want to allow the player to save his progress. So I need to save several integers and a string. And my problem starts here "How can I save integers and load them?". I read the tutorial but I dont understand. Can anyone help me to learn or write me function to save game?
closed account (18hRX9L8)
What you can do is print the values of the integers and strings using fprintf and then get the values back by scanning for them in order using fscanf.

fprintf: http://www.cplusplus.com/reference/cstdio/fprintf/
fscanf: http://www.cplusplus.com/reference/cstdio/fscanf/
Last edited on
closed account (1v5E3TCk)
Thanks I will look it
closed account (1v5E3TCk)
If I said I dont understand would you be angry with me? ):
closed account (1v5E3TCk)
I got it Thanks
Why do you keep advocating C stuff, particularly dangerous and difficult-for-beginners functions like fprintf() and fscanf()?

Particularly when it is easier to use the C++ methods?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
struct player_t
  {
  int power_level;
  int hp;
  int gold_coins;
  ...

  void save_to_file( string filename ) const
    {
    ofstream f( filename.c_str() );
    f << power_level << endl;
    f << hp << endl;
    f << gold_coins << endl;
    }

  bool load_from_file( string filename )
    {
    ifstream f( filename.c_str() );
    f >> power_level;
    f >> hp;
    f >> gold_coins;
    return f.good();
    }
  };
 
player.save_to_file( "user_scores.txt" );
1
2
3
4
if (!player.load_from_file( "user_scores.txt" ))
  {
  showmessage( "Fooey, I could not restore your game!" );
  }


If you want to get fancy and use INI files for multiple players and the like (and you are on Windows), just #include <windows.h> and use the functions Disch explains here:
http://www.cplusplus.com/forum/beginner/30814/#msg166989

Hope this helps.
closed account (1v5E3TCk)
Am I not need to include fstream?
And open and close the file?

I code like that:
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
#include <iostream>
#include <fstream>

using namespace std;

int main()
{
    string x = "Arda";
    string y;
    int a = 5;
    int b;
    int c = 10;
    int d;

    ofstream deneme;
    ifstream denemex;

    deneme.open ( "deneme.dat", ios::binary );

    deneme.write((char*)&x, sizeof(string));
    deneme.write((char*)&a, sizeof(int));
    deneme.write((char*)&c, sizeof(int));

    deneme.close();

    denemex.open ( "deneme.dat", ios::binary );



    denemex.read((char*)&y, sizeof(string));
    denemex.read((char*)&b, sizeof(int));
    denemex.read((char*)&d, sizeof(int));

    denemex.close();


    cout<< y;
    cout<< b;
    cout<< d;

    return 0;
}
closed account (1v5E3TCk)
I got it you wrote a structure
closed account (1v5E3TCk)
Okey I code 2 functions, one for saving and one for loading. But when I try to save the program stops. And when I look into save file there are blank lines.
When I try to load game ıt doesnt load after 6.line.

func_savegame()
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
void func_savegame()
{
    ofstream save( wrath.c_str());

    save << player.name << endl;

    save << player.equip.weapon.name << endl;
    save << player.equip.weapon.attck << endl;
    save << player.equip.weapon.defense << endl;
    save << player.equip.weapon.magic << endl;
    save << player.equip.weapon.price << endl;
    save << player.equip.weapon.type << endl;

    save << player.equip.armor.name << endl;
    save << player.equip.armor.attck << endl;
    save << player.equip.armor.defense << endl;
    save << player.equip.armor.magic << endl;
    save << player.equip.armor.price << endl;
    save << player.equip.armor.type << endl;

    save << player.equip.shield.name << endl;
    save << player.equip.shield.attck << endl;
    save << player.equip.shield.defense << endl;
    save << player.equip.shield.magic << endl;
    save << player.equip.shield.price << endl;
    save << player.equip.shield.type << endl;

    save << player.level << endl;
    save << player.level_hp << endl;
    save << player.level_mp << endl;
    save << player.level_attck << endl;

    save << player.equip_hp << endl;
    save << player.curr_hp << endl;
    save << player.max_hp << endl;

    save << player.equip_mp << endl;
    save << player.curr_mp << endl;
    save << player.max_mp << endl;

    save << player.curr_attck << endl;
    save << player.equip_attck << endl;

    save << player.curr_exp << endl;
    save << player.max_exp << endl;

    save << player.max_temper << endl;
    save << player.curr_temper << endl;

    save << player.stats << endl;
    save << player.sp << endl;

    for( int i = 0; i < 10; ++i )
    {
        save << player.skills[ i ].name << endl;
        save << player.skills[ i ].attck << endl;
        save << player.skills[ i ].defense << endl;
        save << player.skills[ i ].magic << endl;
        save << player.skills[ i ].time << endl;
        save << player.skills[ i ].learn << endl;
    }

    for( int i = 0; i < 10; ++i)
    {
        save << player.num_pot[ i ] << endl;
    }

    save << player.gold << endl;

    for( int i = 0; i < 10; ++i)
    {
        save << player.inventory[ i ].name << endl;
        save << player.inventory[ i ].price << endl;
        save << player.inventory[ i ].attck << endl;
        save << player.inventory[ i ].defense << endl;
        save << player.inventory[ i ].magic << endl;
        save << player.inventory[ i ].type << endl;
    }

    save << player.type << endl;

    save << chapter << endl;
}


func_loadgame()
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
bool func_loadgame()
{

    ifstream load( wrath.c_str());

    getline( load, player.name);

    getline( load, player.equip.weapon.name);
    load >> player.equip.weapon.attck;
    load >> player.equip.weapon.defense;
    load >> player.equip.weapon.magic;
    load >> player.equip.weapon.price;
    load >> player.equip.weapon.type;

    getline(load, player.equip.armor.name);
    load >> player.equip.armor.attck;
    load >> player.equip.armor.defense;
    load >> player.equip.armor.magic;
    load >> player.equip.armor.price;
    load >> player.equip.armor.type;

    getline(load, player.equip.shield.name);
    load >> player.equip.shield.attck;
    load >> player.equip.shield.defense;
    load >> player.equip.shield.magic;
    load >> player.equip.shield.price;
    load >> player.equip.shield.type;

    load >> player.level;
    load >> player.level_hp;
    load >> player.level_mp;
    load >> player.level_attck;
    load >> player.equip_hp;
    load >> player.curr_hp;
    load >> player.max_hp;
    load >> player.equip_mp;
    load >> player.curr_mp;
    load >> player.max_mp;
    load >> player.curr_attck;
    load >> player.equip_attck;
    load >> player.curr_exp;
    load >> player.max_exp;
    load >> player.max_temper;
    load >> player.curr_temper;
    load >> player.stats;
    load >> player.sp;

    for( int i = 0; i < 10; ++i )
    {
        getline( load, player.skills[ i ].name);
        load >> player.skills[ i ].attck;
        load >> player.skills[ i ].defense;
        load >> player.skills[ i ].magic;
        load >> player.skills[ i ].time;
        load >> player.skills[ i ].learn;
    }

    for( int i = 0; i < 10; ++i)
    {
        load >> player.num_pot[ i ];
    }

    load >> player.gold;

    for( int i = 0; i < 10; ++i)
    {
        getline(load, player.inventory[ i ].name);
        load >> player.inventory[ i ].price;
        load >> player.inventory[ i ].attck;
        load >> player.inventory[ i ].defense;
        load >> player.inventory[ i ].magic;
        load >> player.inventory[ i ].type;
    }

    load >> player.type;

    load >> chapter;

    return load.good();
}


Save File

arda
Broken sword
5
0
4
0
1
Damaged armor
0
3
3
0
2
Damaged shield
0
2
0
0
3
1
25
10
5
5
30
30
7
17
17
10
5
55
220
100
5
0
0
0
0
0

0
0
100
0
100
Fast Fist
0
1
0
0
10
Stunning
1
0
0
0
20
Iron Wall
1
0
80
0
30
Blade Rush
0
0
0
0
0	

0
0
0
0
0

0
0
0
0
0

0
0
0
0
0

0
0
0
0
0

0
0
10
3
3
0
0
0
0
0
0
0
7
none
0
0
0
0
0
none
0
0
0
0
0
none
0
0
0
0
0
none
0
0
0
0
0
none
0
0
0
0
0
none
0
0
0
0
0
none
0
0
0
0
0
none
0
0
0
0
0
1
1
Last edited on
Topic archived. No new replies allowed.