Sign in to follow this  

WriteFile() errors

This topic is 4693 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Well, I need to save highscores to a .dat file: [code] 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[i], _iHighscores[i]); 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?

Share this post


Link to post
Share on other sites
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[i]' 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

Share this post


Link to post
Share on other sites
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[i] 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.

Share this post


Link to post
Share on other sites
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:
 WriteFile(hFile, cData[][i] /* this didn't work */, ...); 


So instead I used the alternative: string class.

Thanks!

Share this post


Link to post
Share on other sites
you could use the stl file class ofstream.


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!

Share this post


Link to post
Share on other sites
yes that works but I'm trying to avoid DOS solutions, sticking to the win32 api. ofstream works through DOS, and can only open 8 files at a time. if for some reason other applications are hogging up all 8, there would be a run-time error.

Thanks, though.

Share this post


Link to post
Share on other sites

This topic is 4693 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this