【发布时间】:2011-10-19 04:44:48
【问题描述】:
我的 C++ winAPI 应用程序需要在将文件上传到服务器之前创建一个临时文件。所以我搜索了创建临时文件的方法,发现有很多方法可以做到这一点。
您能告诉我:对于以下每种方法,我应该在哪种情况下使用该方法?哪种方法最适合我的需求?
方法一:
// Using CreateFile()
CreateFile( "myfile.txt", GENERIC_ALL, ..., FILE_ATTRIBUTE_TEMPORARY, 0); // removed unecessary parameters
方法二:
// I think that GetTempFileName also creates the file doesn't it? Not just generates a unique fileName?
// Gets the temp path env string (no guarantee it's a valid path).
dwRetVal = GetTempPath(MAX_PATH, // length of the buffer
lpTempPathBuffer); // buffer for path
// Generates a temporary file name.
uRetVal = GetTempFileName(lpTempPathBuffer, // directory for tmp files
TEXT("DEMO"), // temp file name prefix
0, // create unique name
szTempFileName); // buffer for name
方法三:
// Create a file & use the flag DELETE_ON_CLOSE. So its a temporary file that will delete when the last HANDLE to it closes
HANDLE h_file = CreateFile( tmpfilename, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_FLAG_DELETE_ON_CLOSE, NULL );
为什么创建临时文件的方法不止一种。例如,我想在什么情况下使用方法 2 而不是方法 1?
【问题讨论】:
标签: c++ winapi temporary-files