-
Notifications
You must be signed in to change notification settings - Fork 16
/
file_delete_on_close.c
67 lines (60 loc) · 2.33 KB
/
file_delete_on_close.c
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
//#include <fileapi.h>
//#include <WinBase.h> //FILE_FLAG_DELETE_ON_CLOSE
#include <windows.h> //https://stackoverflow.com/questions/4845198/fatal-error-no-target-architecture-in-visual-studio
#include <io.h> //_open_osfhandle
#include <fcntl.h> //_O_RDONLY, _O_WRONLY
#include <stdio.h>
//https://stackoverflow.com/questions/12249610/c-create-file-in-memory
//https://docs.microsoft.com/zh-tw/windows/win32/fileio/creating-and-using-a-temporary-file
FILE* createTempFile() {
TCHAR szTempFileName[MAX_PATH] = { 0 };
TCHAR lpTempPathBuffer[MAX_PATH] = { 0 };
DWORD dwRetVal = GetTempPath(MAX_PATH, // length of the buffer
lpTempPathBuffer);
if (dwRetVal > MAX_PATH || (dwRetVal == 0)) {
printf("GetTempPath failed\n");
return NULL;
}
//wprintf(L"temp path: %s\n", lpTempPathBuffer);
//temp path: C:\Users\xxx\AppData\Local\Temp\
UINT uRetVal = GetTempFileName(lpTempPathBuffer, // directory for tmp files
TEXT("DEMO"), // temp file name prefix
0, // create unique name
szTempFileName); // buffer for name
if (uRetVal == 0) {
printf("GetTempFileName failed\n");
return NULL;
}
//wprintf(L"temp name: %s\n", szTempFileName);
//temp name: C:\Users\xxx\AppData\Local\Temp\DEM6C25.tmp
HANDLE hTempFile = CreateFile((LPTSTR)szTempFileName, // file name
GENERIC_READ | GENERIC_WRITE, // open for write
0, // do not share
NULL, // default security
CREATE_ALWAYS, // overwrite existing
FILE_ATTRIBUTE_TEMPORARY | FILE_FLAG_DELETE_ON_CLOSE,// normal file
NULL); // no template
//https://stackoverflow.com/questions/5193579/how-make-file-from-handle-in-winapi
int nHandle = _open_osfhandle((long)hTempFile, _O_RDONLY | _O_WRONLY);
if (nHandle == -1) {
::CloseHandle(hTempFile); //case 1
return nullptr;
}
FILE* fp = _fdopen(nHandle, "w+");
if (!fp) {
::CloseHandle(hTempFile); //case 2
}
return fp;
}
int main() {
FILE* fp = createTempFile();
char buffer[] = { 'H','e','l', 'l','o' };
if (fp == NULL) {
printf("open failure");
return 1;
}
fwrite(buffer, 1, sizeof(buffer), fp);
fflush(fp);
fclose(fp);
return 0;
}