C++: Getting a temporary file, cross-platform
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.
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.
If you use Qt: QTemporaryFile class is perfect.
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);
Since C++17, you can use std::filesystem::temp_directory_path().
You can use the C Standard Library function tmpfile
.
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.
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.
精彩评论