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 some crappy named Internet Explorer temp dir.
我正在寻找一个跨平台的方法来指定一个临时文件。例如,在linux中,会出现在/tmp目录中,或者在一些名为Internet Explorer temp dir的糟糕的Windows中。
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)或程序正常终止时,将自动删除创建的临时文件。
6 个解决方案
#1
68
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文件系统库(来自该库的第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 temp
can be used to open a file or create a subdirectory, while the string object tempstr
offers the same information as a string. See http://www.boost.org for more details.
文件系统路径对象temp可用于打开文件或创建子目录,而字符串对象tempstr提供与字符串相同的信息。详情请参见http://www.boost.org。
#3
5
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);
#4
4
You can use the C Standard Library function tmpfile
.
您可以使用C标准库函数tmpfile。
#5
2
Edit: Espeically since you seem to like the idea of Boost, Robbie Morrison's answer is 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)可能已经崩溃、bug、脑死亡,甚至还没有实现。
How about tmpnam, if you don't like tmpfile?
如果你不喜欢tmpfile, tmpnam怎么样?
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.
我认为,如果这是一种担忧,那就没有那么安全了。这里有一个链接描述其中的一些问题。
#6
0
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.
如果您坚持使用文件,请您的程序使用一个名称,可能基于启动时间。
#1
68
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文件系统库(来自该库的第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 temp
can be used to open a file or create a subdirectory, while the string object tempstr
offers the same information as a string. See http://www.boost.org for more details.
文件系统路径对象temp可用于打开文件或创建子目录,而字符串对象tempstr提供与字符串相同的信息。详情请参见http://www.boost.org。
#2
#3
5
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);
#4
4
You can use the C Standard Library function tmpfile
.
您可以使用C标准库函数tmpfile。
#5
2
Edit: Espeically since you seem to like the idea of Boost, Robbie Morrison's answer is 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)可能已经崩溃、bug、脑死亡,甚至还没有实现。
How about tmpnam, if you don't like tmpfile?
如果你不喜欢tmpfile, tmpnam怎么样?
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.
我认为,如果这是一种担忧,那就没有那么安全了。这里有一个链接描述其中的一些问题。
#6
0
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.
如果您坚持使用文件,请您的程序使用一个名称,可能基于启动时间。