BOOL WriteHighscores()
{
HANDLE hFile = CreateFile("HiScores.dat", GENERIC_WRITE, 0, NULL,
CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
if(hFile == INVALID_HANDLE_VALUE)
return false;
for(int i = 0; i < 5; i++)
{
char cData[9];
wsprintf(cData, "%s%05d", _cHighscores, _iHighscores);
DWORD dwBytesWritten;
if(!WriteFile(hFile, &cData, 8, &dwBytesWritten, NULL))
{
CloseHandle(hFile);
return FALSE;
}
}
return CloseHandle(hFile);
}
[\code]
But it always comes up with an Invalid Handle error (0xc0000008) at runtime.
The file is also corrupted in the proccess.
Any suggestions?
WriteFile() errors
Well, I need to save highscores to a .dat file:
A few things -
1. char cData[9]; is very small, try making it a larger number to make sure no overflows happen. Try 256, even if you are not going to use that amount. Also if you change that, make a call to memset(cData,0,256) to clear out the buffer.
2. What is '_cHighscores' defined as? You are using it in the cData as a string ("%s"). This is what led me to suggest #1.
3. WriteFile(hFile, &cData, 8, &dwBytesWritten, NULL)might need to be changed if 1 and 2 are wrong, you may need to try WriteFile(hFile, &cData, strlen(cData), &dwBytesWritten, NULL) instead.
Give those a try first and see if that helps.
- Drew
1. char cData[9]; is very small, try making it a larger number to make sure no overflows happen. Try 256, even if you are not going to use that amount. Also if you change that, make a call to memset(cData,0,256) to clear out the buffer.
2. What is '_cHighscores' defined as? You are using it in the cData as a string ("%s"). This is what led me to suggest #1.
3. WriteFile(hFile, &cData, 8, &dwBytesWritten, NULL)might need to be changed if 1 and 2 are wrong, you may need to try WriteFile(hFile, &cData, strlen(cData), &dwBytesWritten, NULL) instead.
Give those a try first and see if that helps.
- Drew
1. No overflow - that error occured yesterday and I fixed that. cData used
to be a size of [8] now it is [9].
2. _cHighscores is defined as _cHighscores[3][5] storing five 3-letter names, like those highscores on old arcade-games.
3. No that didn't work either
Thanks for your suggestions anyway, though.
to be a size of [8] now it is [9].
2. _cHighscores is defined as _cHighscores[3][5] storing five 3-letter names, like those highscores on old arcade-games.
3. No that didn't work either
Thanks for your suggestions anyway, though.
Ok if I am not mistaken then,
_cHighscores[3][5] is 3 values with 5 letters
I think it should be _cHighscores[5][3]
It goes, rows, then cols. Give that a try as well.
- Drew
_cHighscores[3][5] is 3 values with 5 letters
I think it should be _cHighscores[5][3]
It goes, rows, then cols. Give that a try as well.
- Drew
Well, I've decided to use STL instead, and it worked. (I just have to get the int to string conversion right). When you mentioned that it should be an array of [3][5], a light bulb lit up in my head. I didn't know how to reference each string individually:
So instead I used the alternative: string class.
Thanks!
WriteFile(hFile, cData[] /* this didn't work */, ...);
So instead I used the alternative: string class.
Thanks!
you could use the stl file class ofstream.
Then read it back in using ifstream:
Hope that helps!
std::ofstream ofs("scores.txt");std::string player_name;player_name = "Player 1";if (ofs.good()){ ofs << player << "\t" << score << std::endl;}
Then read it back in using ifstream:
while (ifs.getline(buffer, 512).good()){ std::string line(buffer); // Chop the line up and convert score to int using atoi}
Hope that helps!
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement