Hi,
I'm trying to add some code to my application that saves a highscore out to a text file. I've already written the code that loads the high score in without a problem, which looks like this...
// Create a file pointer for opening and reading files
std::ifstream isGetData;
// Open the .dat file that contains the data
isGetData.open( "HighScore.dat" );
// Check to ensure file was found
if( isGetData.fail() )
{
MessageBoxA( m_hWnd, "Failed to find data file HighScore.dat", "Error!", MB_OK | MB_ICONEXCLAMATION );
}
// Return pointer to the beginning of the file and clear the end flag
isGetData.seekg( NULL, std::ios::beg );
isGetData.clear();
// Create a temporary pointer for reading in data
std::string strLine = "";
std::string strTemp = "";
unsigned short int usiHighScore = 0;
std::string strHighScore = "";
while( getline( isGetData, strLine, '\n' ) )
{
if ( strLine == "<HighScoreNumber>" )
{
isGetData >> usiHighScore;
}
if ( strLine == "<HighScoreText>" )
{
isGetData >> strHighScore;
}
}
// Create a new stringstream to hold the new score without the colon
std::stringstream ssNewScore;
ssNewScore << m_iFirstDigit << m_iSecondDigit << m_iThirdDigit << m_iForthDigit;
// Convert the new score to an unsigned short int
unsigned short int usiNewScore = atoi( ssNewScore.str().c_str() );
m_pText->DrawFont( 150, 100, 300, 300, "HIGHEST SCORE: " + strHighScore );
m_pText->DrawFont( 150, 220, 300, 300, "NEW TIME: " + m_ssClock.str() );
// Compare the new score to the current high score to see if it has been beaten
if( usiNewScore > usiHighScore )
{
m_pText->DrawFont( 150, 330, 300, 300, "Well Done You Have Set" );
m_pText->DrawFont( 150, 370, 300, 300, "A New High Score!" );
}
else
{
m_pText->DrawFont( 150, 330, 300, 300, "Unlucky Better Luck Next" );
m_pText->DrawFont( 150, 370, 300, 300, "Time!" );
}
What I need to do now is either replace the high score value stored in HighScore.dat or overwrite it completely. The .dat file has the following format...
<HighScoreNumber>
0055
<HighScoreText>
00:55
I have started by creating an ofstream in the following manner:
std::ofstream ofSaveData("HighScore.dat", std::ios::binary);
I then tried using ofSaveData.write(); to write stuff to the text file, however I passed the function a stringstream using .str().c_str() but couldn't figure out how to get text on to different lines. Also when I passed the string stream into the .write function I used sizeof(theStringStream) which added a lot of random characters to the end.
Any help would be much appreciated cheers!