class CFileStorage
{//this class writes onto the file Name&Wins.txt
public:
void Read()
{
//the initilisation to open the file is this section
//this enables the file reading to take place
ifstream qGameSave("Name&Wins.txt");
std::string qLine;
//this statements tests whether the file has been open or note
//if the file has been openned,
if (qGameSave.good())
{
//the statement in side the while loop below means that the loop will
//only continue if the end of file(eof) hasn't been reached
while(!qGameSave.eof())
{
//this function reads a line from the file and then stores it onto the srting object qLine
getline(qGameSave, qLine);
//this section prints the lines aquired by the std::string qLine
std::cout << qLine <<std::endl;
}
}
else
{ std::cout<<"error unable to open GameSave file"<<std::endl; }
//close the file, this important since the file could remain in the memory
qGameSave.close();
//the file is then closed after this
}
void SaveGame(std::string xPlayer, int iWins)
{
usingnamespace std;
ofstream qGameSave("Name&Wins.txt");
//the while loop will only work whist the file is openned,
//this helps to prevent errors that may arise a code section tries to execute an unavaible objects
if (qGameSave.good())
{
//below are the messages to be included after the previous game
std::string qLine1("PREVIOUS GAME SAVED:");
std::string qLine2("PLAYER NAME ");
std::string qLine3("PLAYER WINS OUT OF 10 ARE, ");
//this section is used for writing the messages on to the text file saving the player's gaming results
qGameSave << qLine1<<endl;
qGameSave << qLine2 << xPlayer << endl;
qGameSave << qLine3 << iWins << endl;
if (iWins >= 5)
{
//the win case is true, this message will be written
std::string qLine4("WON THIS GAME");
qGameSave << qLine4<< endl;
}
else
{
std::string qLine5("LOST THE GAME");
qGameSave << qLine5<< endl;
}
}
//close the file, this important since the file could remain in the memory
}
};