Reputation: 2893
I am writing a C++ problem. It need to work on both Windows and Unix OS.
How to get user or system tmp folder on different OS?
Upvotes: 35
Views: 40870
Reputation: 119
According to the docs, the max path is MAX_PATH (260). If the path happens to be 260, the code in the sample above (als plougy) will fail because 261 will be returned. Probably the buffer size should be MAX_PATH + 1.
TCHAR szPath[MAX_PATH + 1];
DWORD result = GetTempPath(MAX_PATH + 1, szPath);
if (result != ERROR_SUCCESS) {
// check GetLastError()
}
Upvotes: 0
Reputation: 8627
Update: Thanks @RoiDanton, the most up to date answer is std::filesystem::temp_directory_path
(C++17)
Try boost::filesystem
's temp_directory_path()
which internally uses:
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 GetTempPath
API function.
Interestingly, Window's GetTempPath
uses 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.
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 getenv
function, especially if you want specific order prioritization/requirements or dont want to use another library.
Upvotes: 54
Reputation: 143
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):
//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\
Upvotes: -1
Reputation: 4519
On Windows: Use GetTempPath() to retrieve the path of the directory designated for temporary files.
wstring TempPath;
wchar_t wcharPath[MAX_PATH];
if (GetTempPathW(MAX_PATH, wcharPath))
TempPath = wcharPath;
Upvotes: -1
Reputation: 814
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
bash a_launcher.sh
where a_launcher.sh is like
./a.out /tmp
Upvotes: -1
Reputation: 26940
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.
Upvotes: -1
Reputation: 1025
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).
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.
Upvotes: 4
Reputation: 755094
Use the $TMPDIR
environment variable, according to POSIX.
char const *folder = getenv("TMPDIR");
if (folder == 0)
folder = "/tmp";
Upvotes: 10