I am trying to save player data to a txt file in the follownig format, but the problem is that i dont know how to save a single stat(ex. playerclass), and i dont know how to load into variables. Help!!
void Engine::SaveCharacterData(string PlayerName, int PlayerGender, int PlayerClass, int PlayerSkin)
{
ofstream myfile ("Player1.txt");
if (myfile.is_open())
{
myfile <<"Name:"<< PlayerName;
myfile <<"Gender:"<< PlayerGender;
myfile <<"Class:"<< PlayerClass;
myfile <<"Skin:"<< PlayerSkin;
myfile.close();
}
else
cout << "Unable to open file";
}
void Engine::SaveCharacterClass(int PlayerClass)
{
string curLine;
ofstream myfile ("Resources/Saved_data/Player1.txt");
if (myfile.is_open())
{
getline(myfile, curLine);
if(curLine == "Class=")
{
myfile << PlayerClass;
}
myfile.close();
}
else
cout << "Unable to open file";
}
myfile <<"Name:"<< PlayerName;
myfile <<"Gender:"<< PlayerGender;
This code will create a file that looks like this
Name:GeorgeGender:10
When you save to the file, just insert a blank space or return character after the value, like this
myfile <<"Name:"<< PlayerName << std::endl;
myfile <<"Gender:"<< PlayerGender << std::endl;
Now the saved file will look like this
Name:George
Gender:10
To load data, just get the string from the ":" character to the end using substr (http://www.cplusplus.com/reference/string/string/substr/)