# WriteFile() errors

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

## Create an account

Register a new account

• ### Forum Statistics

• Total Topics
627716
• Total Posts
2978783

• 9
• 21
• 14
• 12
• 42