Runtime Error with ifstream

Ive tackled every solution I could thing of, but I just cant beat it.

My problem is that the ifstream does not load a file when the file exist in the same folder. For instance, if I had a file named Alexstt.txt in the same folder, and I tried to load it with this program to extract the 3 integers (goldCoins, ironOre, and pickaxes) and display the name, it fails to load it. It goes right along to the else statement.

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

using namespace std;

string playerName;
string loadPlayerName;
int goldCoins;
int ironOre;
int pickaxes;

string getName()
{
    string inputName;
    cout << "What is your characters name?" << endl;
    cin.ignore();
    getline(cin, inputName);
    return inputName;
}


void loadChar()
{
    system("cls");

    loadPlayerName = getName();
    string loadAs = loadPlayerName + "stt.txt";

    ifstream getld;
    getld.open(loadAs.c_str(), ios::in);

    if(getld.is_open())
    {
        int stats[2];
        int i = 0;
        while(!getld.eof())
        {

            getld >> stats[i];
            i++;
        }

        goldCoins = stats[0];
        ironOre = stats[1];
        pickaxes = stats[2];
        playerName = loadPlayerName;

        system("cls");
        cout << "The following character has been loaded." << endl << endl;
        cout << playerName << endl << endl;
        cout << "Gold Coins: " << goldCoins << endl;
        cout << "Iron Ore:   " << ironOre << endl;
        cout << "Pickaxes:   " << pickaxes << endl;



    }

    else
    {
        cout << "Sorry, that character does not exist, or could not be loaded." << endl;
    }

    getld.close();


    cout << "Press ENTER to continue..." << endl;
    cin.ignore();
    cin.get();
}

int main()
{
    loadChar();

    return 0;
}
The problem is here:
1
2
3
4
5
6
7
8
string getName()
{
    string inputName;
    cout << "What is your characters name?" << endl;
    cin.ignore(); //This line is a problem - remove it
    getline(cin, inputName);
    return inputName;
}
I removed it and recompiled it. When I played it, after I entered the name, it crashed. This portion of a program is actually part of a larger program, but on this test program it would crash without the cin.ignore(); and on the larger program, it crashes with the cin.ignore();, but with it, it doesn't print my lines of stats.
Last edited on
It's probably the way you are loading from the file:
I used an empty file for the test - you are reading your file - but the way this part
is written
1
2
3
4
5
6
7
8
9
10
11
12
13
        int stats[2]; //stats only has array index of 0 and 1
        int i = 0;
        while(!getld.eof())
        {
          //probably got an array out of bound write  somewhere here 
            getld >> stats[i];
            i++;
        }
        goldCoins = stats[0];
        ironOre = stats[1];
        pickaxes = stats[2];//error - array out of bounds read access
        playerName = loadPlayerName;


Ahh yes, I see, Ill change it to a vector and see how I can make it more efficient and safe. But my file does have 3 integers in it. Ill post my save codes.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
void firstsaveGame()
{
    string saveAs = "C:\\Documents and Settings\\Administrator\\My Documents\\My Games\\RPG\\" + playerName + "stt.txt";
    ofstream saveChar;
    saveChar.open(saveAs.c_str());

    if(saveChar.is_open())
    {
        Logfile(playerName, " has been created.\n\n"); //This is just a logging function. It works.
        saveChar << goldCoins << endl;
        saveChar << ironOre << endl;
        saveChar << pickaxes << endl;
        cout << "Your character has been created..." << endl;
    }

    else
    {
        CreateDirectory("C:\\Documents and Settings\\Administrator\\My Documents\\My Games\\RPG", NULL);
        cout << "Sorry, please try again.\n";
    }

    saveChar.close();
}
If your file has 3 integers - then your array should have been
int stats[3]

P.S
You can just use getld >> stats[0] >> stats[1] >> stats[2]; instead of that 'dodgy' loop.

or do away with the array entirely and just have
getld >> goldCoins >> ironOre >> pickaxes;

Like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
    if(getld.is_open())
    {
        getld >> goldCoins >> ironOre >> pickaxes;
        playerName = loadPlayerName;

        system("cls");
        cout << "The following character has been loaded." << endl << endl;
        cout << playerName << endl << endl;
        cout << "Gold Coins: " << goldCoins << endl;
        cout << "Iron Ore:   " << ironOre << endl;
        cout << "Pickaxes:   " << pickaxes << endl;
   }
Last edited on
Oh wow, thanks for the help! I wasnt too sure about arrays and I though array[2] would mean 3 allocation slots, 0, 1, and 2. I was also a bit iffy on ifstream and the fact that it goes down the line just went over my head. Works like a charm.
Last edited on
Topic archived. No new replies allowed.