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.
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();
}
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();
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;