WriteFile() errors

Started by
5 comments, last by geekalert 19 years, 2 months ago
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?
A JPEG is worth a thousand and twenty four DWORD's. ;)
Note: Due to vacationing my website will not be updated till late-August. Of course I still have internet, but who wants to program during a vacation?
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
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.
A JPEG is worth a thousand and twenty four DWORD's. ;)
Note: Due to vacationing my website will not be updated till late-August. Of course I still have internet, but who wants to program during a vacation?
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
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!
A JPEG is worth a thousand and twenty four DWORD's. ;)
Note: Due to vacationing my website will not be updated till late-August. Of course I still have internet, but who wants to program during a vacation?
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!
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.
A JPEG is worth a thousand and twenty four DWORD's. ;)
Note: Due to vacationing my website will not be updated till late-August. Of course I still have internet, but who wants to program during a vacation?

This topic is closed to new replies.

Advertisement