WriteFile() fails with error ERROR_INVALID_HANDLE

I want to save an array of two ints into a file. The code below is supposed to create a file named "foo.txt" in the executable's directory and write the values of foo and bar into them.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#ifdef _UNICODE
typedef wstring tstring;
#define to_tstring(x) to_wstring(x)
#else
typedef string tstring;
#define to_tstring(x) to_string(x)
#endif

const int bytesToSave = sizeof(int) * 2;

TCHAR path[MAX_PATH];
GetModuleFileName(NULL, path, MAX_PATH);
PathRemoveFileSpec(path);
tstring temp(path);
temp += _T("\\foo.txt");
HANDLE file = CreateFile(temp.c_str(), GENERIC_WRITE, NULL, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
DWORD written;
int save[2] = {
	foo,
	bar
};
if (!WriteFile(file, save, bytesToSave, &written, NULL)) {
	DWORD errorCode = GetLastError();
	tstring errorMessage = _T("Failed to save current status! Error Code: ");
	errorMessage += to_tstring(errorCode);
	MessageBox(hwnd, errorMessage.c_str(), _T("ERROR!"), MB_ICONERROR);
}
CloseHandle(file);


But the MessageBox pops up saying "Failed to save current status! Error Code: 6". I checked the docs of msdn and found out that error code 6 is ERROR_INVALID_HANDLE, but I don't see anything wrong with my call to CreateFile(). What's wrong with this code?
closed account (48bpfSEw)
first: you should handle all errors!

http://stackoverflow.com/questions/28386062/window-createfile-readfile-writefile



The code works fine for me. Maybe you don't have write permission.
What directory is your program located?
@Thomas1965 I'm using Visual Studio 2015, so the executable is located in Documents\Visual Studio 2015\Projects\Project_Name\Debug\Project_Name.exe

There shouldn't be any access violation here... The part from TCHAR path[MAX_PATH] to the end are all in a VOID saveStats(). It is supposed to save the high scores for a game. In the real project foo and bar are two ints that stores the high scores for two games.

And here's something that might be helpful: The code worked before when there is only one game mode, and it only started to fail when I implemented multiple game modes. The code before is basically the same, only some minor changes:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#ifdef _UNICODE
typedef wstring tstring;
#define to_tstring(x) to_wstring(x)
#else
typedef string tstring;
#define to_tstring(x) to_string(x)
#endif

const int bytesToSave = sizeof(int) * 1;

TCHAR path[MAX_PATH];
GetModuleFileName(NULL, path, MAX_PATH);
PathRemoveFileSpec(path);
tstring temp(path);
temp += _T("\\foo.txt");
HANDLE file = CreateFile(temp.c_str(), GENERIC_WRITE, NULL, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
DWORD written;
int save[1] = {
	foo,
};
if (!WriteFile(file, save, bytesToSave, &written, NULL)) {
	DWORD errorCode = GetLastError();
	tstring errorMessage = _T("Failed to save current status! Error Code: ");
	errorMessage += to_tstring(errorCode);
	MessageBox(hwnd, errorMessage.c_str(), _T("ERROR!"), MB_ICONERROR);
}
CloseHandle(file);
Thanks to both Necip and Thomas1965. I have solved the problem. Turns out that the HANDLE CreateFile returned is INVALID_HANDLE_VALUE. And when I call GetLastError(), the error code is 32, which is ERROR_SHARING_VIOLATION.
The problem turned out to be that I read the file before I saved it, and forgot to close the handle. If I add CloseHandle(file) after my file-reading code, which is not shown here, the file could be saved normally. Thanks to all who helped.
Topic archived. No new replies allowed.