Writing to a binary file trouble.

I'm making a game to challenge myself. I've been working on the save function of my player class but I can't seem to get it to work. It's suppose to write the player's stats to a binary file, and the load function is suppose to read them from it. However, it keeps saying 'binfil undeclared'. Im not really good with the whole read/write binary file thing yet. IF you know a better way to do the save/load functions that could help too.

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
#include "Player.h"
#include <iostream>
#include <fstream>
using namespace std;

Player::Player()
{

}
void Player::IncrStat(int s, int d, int a, int h, int e){

    Strg += s;
    Def += d;
    Agil += a;
    Health += h;
    Exp += e;

}

void Player::DecrStat(int s, int d, int a, int h, int e){

    Strg -= s;
    Def -= d;
    Agil -= a;
    Health -= h;
    Exp -= e;

}

void Player::save(){

    ofstream S;
    S.open("SAVE.bin", ios::binary | ios::out);
    binfil.write((char*)(&Strg), sizeof(Strg));
    binfil.write((char*)(&Def), sizeof(Def));
    binfil.write((char*)(&Agil), sizeof(Agil));
    binfil.write((char*)(&Exp), sizeof(Exp));
    fbin.close();
}

void Player::load(){

    ifstream S;
    S.open("SAVE.bin", ios::binary | ios::in);
    binfil.read((char*)(&Strg), sizeof(Strg));
    binfil.read((char*)(&Def), sizeof(Def));
    binfil.read((char*)(&Agil), sizeof(Agil));
    binfil.read((char*)(&Exp), sizeof(Exp));
    fbin.close();
}
Last edited on
Well, maybe binfil is not declared. Isn't your code supposed to be
1
2
3
4
ifstream S;
S.open(...);
S.read(...);
S.close();
and the same for saving?
Oh wow! Can't believe I missed something like that. I had to look the whole binary file thing up in my book and he copying the syntax for it I forgot to change that. Thanks a ton!
Topic archived. No new replies allowed.