C++:获取临时文件,跨平台

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5586355/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 18:27:10  来源:igfitidea点击:

C++: Getting a temporary file, cross-platform

c++temporary-files

提问by orlp

I'm looking for a cross-platform way of getting designated a temporary file. For example in linux that would be in the /tmpdir and in Windows in some crappy named Internet Explorer temp dir.

我正在寻找一种跨平台的方式来指定一个临时文件。例如,在 linux 中,该/tmp目录位于dir 中,而在 Windows 中,则位于一些蹩脚的 Internet Explorer 临时目录中。

Does a cross-platform (Boost?) solution to this exist?

是否存在跨平台(Boost?)解决方案?

EDIT:

编辑

I need this file to exist until the program terminates. tmpfile()does not guarantee that. Quoting from ccpreference:

我需要这个文件存在直到程序终止。tmpfile()不保证。从 ccpreference 引用:

The temporary file created is automatically deleted when the stream is closed (fclose)or when the program terminates normally.

创建的临时文件在流关闭(fclose)或程序正常终止时自动删除。

回答by Robbie Morrison

The Boost Filesystemlibrary, from version 3 of that library, can be used to create a temporary file name. It also offers a crisp solution. Indeed, the following C++ code should be platform independent:

提高文件系统库,该库的版本3,可用于创建一个临时文件名。它还提供了一个清晰的解决方案。实际上,以下 C++ 代码应该与平台无关:

// Boost.Filesystem VERSION 3 required
#include <string>
#include <boost/filesystem.hpp>
boost::filesystem::path temp = boost::filesystem::unique_path();
const std::string tempstr    = temp.native();  // optional

The filesystem path object tempcan be used to open a file or create a subdirectory, while the string object tempstroffers the same information as a string. See http://www.boost.orgfor more details.

文件系统路径对象temp可用于打开文件或创建子目录,而字符串对象tempstr提供与字符串相同的信息。有关更多详细信息,请参阅http://www.boost.org

回答by Naszta

If you use Qt: QTemporaryFileclass is perfect.

如果您使用 Qt:QTemporaryFile类是完美的。

回答by Karel Petranek

The standard C library contains a function called tmpfile, it probably does what you need: http://www.cplusplus.com/reference/clibrary/cstdio/tmpfile/

标准 C 库包含一个名为 的函数tmpfile,它可能满足您的需求:http: //www.cplusplus.com/reference/clibrary/cstdio/tmpfile/

You can use it in C++ programs as well.

您也可以在 C++ 程序中使用它。

EDIT:
If you need only file name, you can use tmpnam, it doesn't delete the file when fclose is called. It returns full file path, including the temp directory.

编辑:
如果您只需要文件名,则可以使用tmpnam,它不会在调用 fclose 时删除文件。它返回完整的文件路径,包括临时目录。

The C way:

C方式:

const char *name = tmpnam(NULL);  // Get temp name
FILE *fp = fopen(name, "w");  // Create the file
// ...
fclose(fp);
remove(name);

回答by James McNellis

You can use the C Standard Library function tmpfile.

您可以使用 C 标准库函数tmpfile

回答by jwd

Edit:Espeically since you seem to like the idea of Boost, Robbie Morrison's answeris probably better for you.

编辑:特别是因为您似乎喜欢 Boost 的想法,Robbie Morrison 的回答可能更适合您。

My original answer remains below, but anyone reading this: please beware that tmpnam is unsafe. Further, some platforms (such as Windows) may have broken, buggy, braindead, or even missing implementations.

我的原始答案仍然在下面,但阅读本文的任何人:请注意 tmpnam 是不安全的。此外,某些平台(例如 Windows)可能已损坏、有问题、脑残,甚至缺少实现。



How about tmpnam, if you don't like tmpfile?

如何使用tmpnam,如果你不喜欢TMPFILE?

From the link:

从链接:

The file created this way, unlike those created with tmpfile is not automatically deleted when closed; You should call remove to delete this file once closed.

以这种方式创建的文件,与使用 tmpfile 创建的文件不同,关闭时不会自动删除;一旦关闭,您应该调用 remove 删除此文件。

Especially if you need another program to know the name of the file, this seems more appropriate, since tmpfile doesn't give you a name at all.

特别是如果您需要另一个程序来知道文件的名称,这似乎更合适,因为 tmpfile 根本不给您名称。

I believe it is not as secure though, if that's a concern. Here is a link describing some of those issues.

我相信它不是那么安全,如果这是一个问题。这是描述其中一些问题链接



Aside: Even if you wanted to use tmpfile, you should consider the more secure tmpfile_s(the MS docseven go so far as to call tmpfile "deprecated" though I doubt it would be removed from the C++ standard any decade soon). Regardless, neither of those retain the named file, which you require.

旁白:即使您想使用 tmpfile,您也应该考虑更安全的tmpfile_sMS 文档甚至将 tmpfile 称为“已弃用”,尽管我怀疑它是否会在未来十年内从 C++ 标准中删除)。无论如何,它们都不会保留您需要的命名文件。

回答by havogt

Since C++17, you can use std::filesystem::temp_directory_path().

从 C++17 开始,您可以使用std::filesystem::temp_directory_path()

回答by Mark B

There are much better ways to communicate between programs than random temporary files. Could you use a pipe for the communication instead? What about using a localhost socket?

有比随机临时文件更好的程序间通信方式。您可以使用管道进行通信吗?使用本地主机套接字怎么样?

If you insist on using files, just have your program use a name, possibly based on startup time.

如果您坚持使用文件,只需让您的程序使用一个名称,可能基于启动时间。