每當我在C++ Native WinAPI中創建臨時文件時,返回的HANDLE總是INVALID_FILE_HANDLE,但臨時文件已被正確創建?創建臨時文件:不能寫入
我做錯了什麼?
如果我使用GetLastError()的輸出是:「文件存在」
在我的代碼下面我可以成功地創建一個臨時文件,但CreateFile函數總是返回INVALID_FILE_HANDLE,我不能寫任何文字文件句柄:
GetTempPath(dirLen, dir);
GetTempFileName(dir, fName, 0, tName);
HANDLE file = CreateFile(tName, GENERIC_WRITE, 0, NULL, CREATE_NEW,
FILE_ATTRIBUTE_TEMPORARY, NULL);
if (file == INVALID_HANDLE_VALUE) {
outputLastError(); // Outputs: "The file exists"
}
if (FAILED(WriteFile(file, (LPTSTR)toWrite.c_str(), strLen, 0, NULL))) {
cout << (_T("Failed to write string to file \r\n"));
outputLastError();
res = false;
}
// WriteFile doesn't fail but the temporary file is empty when I open it??