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

发布于 2024-10-30 22:03:29 字数 342 浏览 2 评论 0原文

我正在寻找一种跨平台的方式来指定临时文件。例如,在 Linux 中,该目录位于 /tmp 目录中,而在 Windows 中,该目录位于类似于 C:\Users\Username\AppData\Local\Temp 的目录中。

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

编辑

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

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

I'm looking for a cross-platform way of getting designated a temporary file. For example in linux that would be in the /tmp dir and in Windows in something akin to C:\Users\Username\AppData\Local\Temp.

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

EDIT:

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

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

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

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

发布评论

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

评论(7

哭了丶谁疼 2024-11-06 22:03:29

Boost Filesystem 库,来自版本 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

文件系统路径对象 temp 可用于打开文件或创建子目录,而字符串对象 tempstr 提供相同的功能信息作为字符串。

The Boost Filesystem library, 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:

// 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 temp can be used to open a file or create a subdirectory, while the string object tempstr offers the same information as a string.

筱武穆 2024-11-06 22:03:29

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

If you use Qt: QTemporaryFile class is perfect.

胡大本事 2024-11-06 22:03:29

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

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

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

C方式:

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

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

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

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.

The C way:

const char *name = tmpnam(NULL);  // Get temp name
FILE *fp = fopen(name, "w");  // Create the file
// ...
fclose(fp);
remove(name);
余厌 2024-11-06 22:03:29

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

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

太阳公公是暖光 2024-11-06 22:03:29

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

You can use the C Standard Library function tmpfile.

Oo萌小芽oO 2024-11-06 22:03:29

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

我原来的答案仍然在下面,但任何读到这篇文章的人:请注意 tmpnam 不安全。此外,某些平台(例如 Windows)可能已损坏、有缺陷、脑死亡,甚至缺少实现。


如果您不喜欢 tmpfile,那么 tmpnam 怎么样?

从链接:

以这种方式创建的文件与
那些用 tmpfile 创建的不是
关闭时自动删除;你
应该调用remove来删除这个文件
一旦关闭。

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

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


旁白:即使您想使用 tmpfile,您也应该考虑更安全的 tmpfile_sMS 文档< /a> 甚至称 tmpfile 为“已弃用”,尽管我怀疑它很快就会从 C++ 标准中删除)。无论如何,它们都不会保留您需要的指定文件。

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

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.


How about tmpnam, if you don't like 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.

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.

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 docs even 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.

私野 2024-11-06 22:03:29

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

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

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.

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