16
System / Input/output with files help
« on: September 18, 2012, 06:30:26 pm »
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";
}
{
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";
}