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

发布于 2024-12-10 19:37:41 字数 1338 浏览 0 评论 0原文

我的 C++ winAPI 应用程序需要在将文件上传到服务器之前创建一个临时文件。所以我搜索了创建临时文件和临时文件的方法。发现有很多方法可以做到这一点。

你能告诉我:对于下面的每一种方法,我应该在什么场景下使用该方法?哪种方法最适合我的需求?

方法 1:

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

方法 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 

方法 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 ); 

为什么有不止一种方法来创建临时文件。举例来说,在什么情况下我会想使用方法 2 而不是方法 1?

My C++ winAPI application has a need to create a temporary file prior to uploading the file to a server. 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:

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

Method 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:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

紫轩蝶泪 2024-12-17 19:37:41

FILE_ATTRIBUTE_TEMPORARY只是告诉Windows,如果有足够的缓存,就不要将文件内容写入磁盘,因为该文件是临时的,没有其他进程会使用它。

FILE_FLAG_DELETE_ON_CLOSE 顾名思义,当您关闭文件时,它会被自动删除。这保证了这将是暂时的。

GetTempFilename 为临时文件创建一个名称,并保证该文件名以前未被使用过。

创建临时文件时应该使用所有 3 种方法。他们都不干扰其他人。

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_FLAG_DELETE_ON_CLOSE means just what it says - when you close the file it will be deleted automatically. 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.

You should use all 3 methods when creating a temporary file. None of them interferes with the others.

橘味果▽酱 2024-12-17 19:37:41

对于方法 #2,如果您使用 0 作为“唯一 ID”,您实际上需要使用 FILE_ATTRIBUTE_TEMPORARY 调用 SetFileAttributes 以使生成的文件成为临时文件,其意义与方法 #1 相同(否则它将是一个普通的 ARCHIVE/NOT_CONTENT_INDEXED 文件。

) GetFileAttributes 或 GetFileInformationByHandle 查看文件实际拥有的属性。

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.)

Use GetFileAttributes or GetFileInformationByHandle to see what attributes the file actually possesses.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文