segmentation fault

Hi, I have a problem with a program I've been writing for the past couple of days. The series of events that lead to noticing the error are:
- start the program.
- create a new character
- quit
- start the program
- load a character
- try to run the game.

I think it has something to do with this function. I think it's the reading from the file that is causing some sort of memory leak.



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
bool GameObj::loadPlayer(string &name)
{
    cout << "Enter your characters name: ";
    cin >> name;

    for (unsigned int i = 0; i < name.length(); i++)
    {
        name[i] = tolower(name[i]);
    }

    string extension = "_save.bin";
    #ifdef WINDOWS
    string path = "save\\";
    #else
    string path = "save/";
    #endif
    string fileName = path + name + extension;

    ifstream file;
    file.open(fileName.c_str(), ios::binary | ios::in);

    cout << "\nLoading...";
    #ifdef WINDOWS
    Sleep(700);
    #else
    sleep(0.7);
    #endif

    if (file.good())
    {
        file.read(reinterpret_cast<char*>(&player), sizeof(player));
        if (file.fail())
        {
            file.close();
            cout << "\ncouldn't read the save file\n";
            #ifdef WINDOWS
            Sleep(700);
            #else
            sleep(0.7);
            #endif

            name = "player";
            return false;
        }
        file.close();

        return true;
    }
    else
    {
        file.close();
        cout << "\nCouldn't open the save file\n";
        #ifdef WINDOWS
        Sleep(1000);
        #else
        sleep(1);
        #endif

        name = "player";
        return false;
    }
}

reinterpret_cast is always the best candidate for a crash. what is player?
It's okay I fixed it.

Topic archived. No new replies allowed.