Having trouble trying to write to a binary file

OK, I've been working on a small game project and I got stuck when trying to
add a 'high score' feature. What I intent to do is store the highest score
registered so far and update it if the player scored higher.
This is how I'm pretending to do it:

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
// blablablabla
// beggining the game and loading everything
std::ifstream file;
file.open("rsc\\hi-score.bin", std::ios::binary);
if (file.is_open())
{
    char *temp = nullptr;
    file.seekg(0, std::ios::end);
    int size = file.tellg();
    file.seekg(0, std::ios::beg);
    file.read(temp, size);
    hiscore = int(*temp);
}
else
    hiscore = 1;
file.close();

// blablablabla
// the game ended
if (score > hiscore)
{
    std::ofstream file("rsc\\hi-score.bin", std::ios::binary);
    if (file.is_open())
        file.write((char*)score, sizeof(score));
    else
        std::cout << "i hate u" << std::endl;
    file.close();
}


However, when I run the program, everything seems fine, the file even creates
successfully at the requested directory. But when it reaches the end of the
game and gets to the "file.write()" part, it crashes with the following output:

Unhandled exception at 0x593F6D46 (msvcp110d.dll) in Super Kanji Challange.exe: 0xC0000005: 
Access violation reading location 0x00000008.


Can somebody explain me what I'm doing wrong?
Last edited on
The first part of the code attempts to use ofstream for input. It should be ifstream.
Last edited on
My bad. That mistake is from transcribing the code, in the original I'm correctly using ifstream
How score is defined ? Using sizeof could be the issue.
Line 24,
 
    file.write((char*)score, sizeof(score))

this attempts to cast the value of score directly as an address, it should use the & operator to get the address of score:
 
    file.write((char*) &score, sizeof(score));


The input code uses a null pointer as the address where the data is to be read into.
It could be fixed by allocating an array of characters of the correct size. but the code looks more complex than necessary.
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
    
    int score = 0;
    int hiscore = 1;

    // blablablabla
    // begining the game and loading everything
    std::ifstream file;
    file.open("rsc\\hi-score.bin", std::ios::binary);
    if (file.is_open())
    {
        file.read((char*) &hiscore, sizeof(hiscore));
        file.close();
    }
    else
        hiscore = 1;


    //------------------------------------------------------------------------

    score =  rand() % 1000;

    //------------------------------------------------------------------------


    // blablablabla
    // the game ended

    if (score > hiscore)
    {
        std::ofstream file("rsc\\hi-score.bin", std::ios::binary);
        if (file.is_open())
        {
            file.write((char*) &score, sizeof(score));
            file.close();
        }
    }

@Chervil:
I tried your code and it worked. Thanks a lot!

Also, thanks for explaining where I made the mistake too. :)
Last edited on
Topic archived. No new replies allowed.