简体   繁体   English

创建临时文件的方法有很多:每种方法背后的情况是什么?

[英]Many ways to create a Temporary file: what is the situation behind each method?

My C++ winAPI application has a need to create a temporary file prior to uploading the file to a server. 我的C ++ winAPI应用程序需要在将文件上传到服务器之前创建一个临时文件。 So I have searched ways to create a temporary file & found there are many ways to do this. 因此,我已经搜索了创建临时文件的方法,并发现有许多方法可以执行此操作。

Can you tell me: For each of the following methods below, in which scenario am I supposed to use that method? 您能否告诉我:对于下面的每种方法,我应该在哪种情况下使用该方法? And which method would best suite my needs? 哪种方法最适合我的需求?

Method 1: 方法1:

// Using CreateFile()
CreateFile( "myfile.txt", GENERIC_ALL, ..., FILE_ATTRIBUTE_TEMPORARY, 0); // removed unecessary parameters

Method 2: 方法2:

// 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 

Method 3: 方法3:

// 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 ); 

Why are there more than 1 way to create a temp file. 为什么有不止一种创建临时文件的方法。 And, for example, what is the situation where I would want to use, say, method 2 over method 1? 而且,例如,在哪种情况下,我想使用方法2而不是方法1?

FILE_ATTRIBUTE_TEMPORARY simply tells Windows not to bother writing the file contents to disk if there's enough cache, because the file is temporary and no other process will be using it. FILE_ATTRIBUTE_TEMPORARY只是告诉Windows如果有足够的缓存,不要费心将文件内容写入磁盘,因为文件是临时文件,其他进程都不会使用它。

FILE_FLAG_DELETE_ON_CLOSE means just what it says - when you close the file it will be deleted automatically. FILE_FLAG_DELETE_ON_CLOSE就是说的意思-关闭文件时,该文件将被自动删除。 This guarantees that it will be temporary. 这样可以保证它是临时的。

GetTempFilename creates a name for a temporary file, and guarantees that the filename hasn't been used previously. GetTempFilename为临时文件创建一个名称,并保证以前未使用过该文件名。

You should use all 3 methods when creating a temporary file. 创建临时文件时,应使用所有3种方法。 None of them interferes with the others. 他们都没有干扰其他人。

For method #2 if you use 0 for the "unique ID" you actually need to call SetFileAttributes with FILE_ATTRIBUTE_TEMPORARY to make the generated file temporary in the same sense as method #1 (otherwise it will be a normal ARCHIVE/NOT_CONTENT_INDEXED file.) 对于方法2,如果将0用作“唯一ID”,则实际上需要使用FILE_ATTRIBUTE_TEMPORARY调用SetFileAttributes,以使生成的文件成为与方法1相同的临时文件(否则它将是普通的ARCHIVE / NOT_CONTENT_INDEXED文件。)

Use GetFileAttributes or GetFileInformationByHandle to see what attributes the file actually possesses. 使用GetFileAttributes或GetFileInformationByHandle可以查看文件实际拥有的属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM