C++:获取临时文件,跨平台
我正在寻找一种跨平台的方式来指定临时文件。例如,在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
Boost Filesystem 库,来自版本 3该库的,可用于创建 临时文件名。它还提供了一个清晰的解决方案。事实上,以下 C++ 代码应该是平台无关的:
文件系统路径对象
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:
The filesystem path object
temp
can be used to open a file or create a subdirectory, while the string objecttempstr
offers the same information as a string.如果您使用 Qt: QTemporaryFile 类是完美的。
If you use Qt: QTemporaryFile class is perfect.
标准 C 库包含一个名为 tmpfile 的函数,它可能可以满足您的需要:http://www.cplusplus.com/reference/clibrary/cstdio/tmpfile/
您也可以在 C++ 程序中使用它。
编辑:
如果您只需要文件名,可以使用
tmpnam
,调用 fclose 时不会删除文件。它返回完整的文件路径,包括临时目录。C方式:
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:
从 C++17 开始,您可以使用 std::filesystem::temp_directory_path()。
Since C++17, you can use std::filesystem::temp_directory_path().
您可以使用 C 标准库函数
tmpfile
。You can use the C Standard Library function
tmpfile
.编辑:特别是因为您似乎喜欢Boost的想法,Robbie Morrison的答案可能更好为你。
我原来的答案仍然在下面,但任何读到这篇文章的人:请注意 tmpnam 不安全。此外,某些平台(例如 Windows)可能已损坏、有缺陷、脑死亡,甚至缺少实现。
如果您不喜欢 tmpfile,那么 tmpnam 怎么样?
从链接:
特别是如果您需要另一个程序来知道文件的名称,这似乎更合适,因为 tmpfile 根本不给您名称。
我相信它并不那么安全,如果这是一个问题的话。
这是描述其中一些问题的链接。
旁白:即使您想使用 tmpfile,您也应该考虑更安全的 tmpfile_s(MS 文档< /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:
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.
程序之间的通信有比随机临时文件更好的方法。您可以使用管道来代替通信吗?使用本地主机套接字怎么样?
如果您坚持使用文件,只需让您的程序使用一个名称,可能基于启动时间。
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.