C++ 如何在 unix 和 windows 中获取系统或用户临时文件夹?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8087805/
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 17:58:29  来源:igfitidea点击:

how to get system or user temp folder in unix and windows?

c++

提问by performanceuser

I am writing a C++ problem. It need to work on both Windows and Unix OS.

我正在写一个 C++ 问题。它需要在 Windows 和 Unix 操作系统上工作。

How to get user or system tmp folder on different OS?

如何在不同的操作系统上获取用户或系统 tmp 文件夹?

回答by Preet Kukreti

Update: Thanks @RoiDanton, the most up to date answer is std::filesystem::temp_directory_path(C++17)

更新:谢谢@RoiDanton,最新的答案是std::filesystem::temp_directory_path(C++17)



Try boost::filesystem's temp_directory_path()which internally uses:

尝试boost::filesystemtemp_directory_path()内部使用:

  • ISO/IEC 9945 (POSIX): The path supplied by the first environment variable found in the list TMPDIR, TMP, TEMP, TEMPDIR. If none of these are found, "/tmp", or, if macro __ANDROID__is defined, "/data/local/tmp"

  • Windows: The path reported by the Windows GetTempPathAPI function.

  • ISO/IEC 9945 (POSIX):由列表中的第一个环境变量提供的路径TMPDIRTMPTEMPTEMPDIR。如果这些都没有找到,"/tmp"或者,如果__ANDROID__定义了宏,"/data/local/tmp"

  • Windows:Windows GetTempPathAPI 函数报告的路径。

Interestingly, Window's GetTempPathuses similar logic to the POSIX version: the first environment variable in the list TMP, TEMP, USERPROFILE. If none of these are found, it returns the Windows directory.

有趣的是,WindowGetTempPath使用与 POSIX 版本类似的逻辑:列表中的第一个环境变量TMP, TEMP, USERPROFILE。如果这些都没有找到,它会返回 Windows 目录。

The fact that these methods primarily rely on environment variables seems a bit yuck. But thats how it seems to be determined. Seeing as how mundane it really is, you could easily roll your own using cstdlib's getenvfunction, especially if you want specific order prioritization/requirements or dont want to use another library.

这些方法主要依赖于环境变量的事实似乎有点令人讨厌。但这似乎是如何确定的。看到如此的平凡如何真的是,你可以很容易滚你自己使用cstdlibgetenv功能,特别是如果你想要特定的顺序优先/需求或不想使用另一个库。

回答by Jonathan Leffler

Use the $TMPDIRenvironment variable, according to POSIX.

$TMPDIR根据 POSIX,使用环境变量。

char const *folder = getenv("TMPDIR");
if (folder == 0)
    folder = "/tmp";

回答by A.H

if you use QT(Core) you can try QString QDir::tempPath(), or use it's implementation in your code (QT is open, so, check how they do).

如果你使用 QT(Core) 你可以尝试QString QDir::tempPath(),或者在你的代码中使用它的实现(QT 是开放的,所以,检查它们是如何做的)。

The doc say : On Unix/Linux systems this is usually /tmp; on Windows this is usually the path in the TEMP or TMP environment variable.

文档说:在 Unix/Linux 系统上,这通常是 /tmp;在 Windows 上,这通常是 TEMP 或 TMP 环境变量中的路径。

回答by FailedDev

Handy function :

方便的功能:

std::string getEnvVar( std::string const & key )
{
    char * val = getenv( key.c_str() );
    return val == NULL ? std::string("") : std::string(val);
}

I guess TEMP or something could be passed as an argument? Depending on the OS of course. getenv is part of stdlib so this should also be portable.

我猜 TEMP 或其他东西可以作为参数传递?当然取决于操作系统。getenv 是 stdlib 的一部分,因此它也应该是可移植的。

回答by ASten

If you get an access to main() function code, may be better is to put necessary folder names through the main()'s **argv and use an OS-dependend batch launcher. For example, for UNIX

如果您可以访问 main() 函数代码,最好通过 main() 的 **argv 放置必要的文件夹名称并使用依赖于操作系统的批处理启动器。例如,对于 UNIX

bash a_launcher.sh

where a_launcher.sh is like

a_launcher.sh 在哪里

./a.out /tmp

回答by Deadlock

On Windows: Use GetTempPath()to retrieve the path of the directory designated for temporary files.

Windows 上:使用GetTempPath()检索为临时文件指定的目录的路径。

wstring TempPath;
wchar_t wcharPath[MAX_PATH];
if (GetTempPathW(MAX_PATH, wcharPath))
  TempPath = wcharPath;

回答by asd plougry

None of these examples are really concrete and provide a working example (besides std::filesystem::temp_directory_path) rather they're referring you to microsoft's documentation, here's a working example using "GetTempPath()" (tested on windows 10):

这些示例都不是真正具体的,并提供了一个工作示例(除了 std::filesystem::temp_directory_path ),而是将您推荐给 microsoft 的文档,这是一个使用“GetTempPath()”的工作示例(在 Windows 10 上测试):

//temp.cpp
#include <iostream>
#include <windows.h>


int main()
{ 
    TCHAR path_buf[MAX_PATH];
    DWORD ret_val = GetTempPath(MAX_PATH, path_buf);
    if ( ret_val > MAX_PATH || (ret_val == 0) )
    {
        std::cout << "GetTempPath failed";
    } else {
        std::cout << path_buf;
    }
}

outputs:

输出:

C:\>temp.exe
C:\Users\username\AppData\Local\Temp\