save file using fstream

im trying to make a save and load function for my text rpg. here what i have.
the fstream is fairly new for me so let me know if im doing some thing wrong.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

    void SaveFileWarrior(int &exp, int &str, int &stam, int &lvl)
    {

    fstream Save;

    Save.open("rpg.save");

     Save >> exp;
     Save >> str;
     Save >> stam;
     Save >> lvl;

    cout << "file saved" << endl;

    Save.flush();
    Save.close();
    }


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
int LoadFileWarrior(int &exp, int &str, int &stam, int &lvl)
    {
        fstream OpenFile;

        OpenFile.open("rpg.save");

        OpenFile << exp;
        OpenFile << str;
        OpenFile << stam;
        OpenFile << lvl;

        cout << "File Loaded" << endl;

        OpenFile.flush();
        OpenFile.close();

        return exp, str, stam, lvl;

    }


also how to i choose the path that the text file will save at? or will it just be in the same folder as the .cpp by default? when i run it i dont see the file in that folder.
Last edited on
personally I prefer using ifstream for input file stream and ofstream for output...

http://cplusplus.com/doc/tutorial/files/

also as general convention you should use camel case for variables and function names.

openFile > OpenFile
loadFileWarrior > LoadFileWarrior etc

but that's just me being very picky :P

also with the ifstream and ofstream, instead of using the .open() function you can simply declare the file within the args when declaring. eg.

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
void saveFileWarrior(int &exp, int &str, int &stam, int &lvl)
    {

    ifstream save("rpg.save");
    string line;
    
    if (! save.is_open())
    {
        cerr << "error opening file";
        exit (1);
    // or whatever handling you want to do....
    }

    int count=0;
// while not end of file grab next line.
    while (! save.eof())
    {
        getline (save, line);
        array[count] = line;
        count++;
    }
// these next declarations won't really work.... better off using an array to store data. 
// or store each line in a diff variable. as this is saying "store the whole file in exp, 
// now store the whole file in str, now store it in stam, and so on...
     save >> exp;
     save >> str;
     save >> stam;
     save >> lvl;

    cout << "file saved" << endl;

    save.close();

    }
Last edited on
when i changed it to ofstream and ifstream it tells me that flush is not a member. do i need flush?
Last edited on
nope, read above i edited to give you a hand.

if you have exp, str, stam, lvl on each line of file then you probably want to split the line up to store in each variable. TBH, I havn't done much string splitting in C++ off the top of my head I can only recall java, but I'm guessing you can probably use string.substring();

http://cplusplus.com/reference/string/string/substr/

or c_str ( which is a little complicated. )

http://cplusplus.com/reference/string/string/c_str/
Last edited on
i almost got it working but now I'm getting one last error. and by the way i made them part of main instead of a function so simplify things. here is the error: no match for 'operator>>' in 'Save >> PlayerLevel. why wont it let me use this operator to input data in the file?

1
2
3
4
5
6
7
8
9
10
 ofstream Save("rpg.save");

           Save >> PlayerEXP;
           Save >> PlayerStrength;
           Save >> PlayerStamina;
           Save >> PlayerLevel;

           Save.close();

          cout << "File Saved" << endl;



Last edited on
Topic archived. No new replies allowed.