Jump to content
  • Advertisement
Sign in to follow this  
geekalert

WriteFile() errors

This topic is 4911 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: 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?

Share this post


Link to post
Share on other sites
Advertisement
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

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 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
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

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[] /* 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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!