std::filesy...ectory_path(3)
π std::filesystem::temp_directory_path
okay bestie, welcome to std::filesystem::temp_directory_path πΈ this one lives in the C++ Standard Libary (their spelling, not mine, i don't make the rules π) and honestly? she's giving "where do my temp files go" and she ATE. no cap.
β¨ NAME β¨
std::filesystem::temp_directory_path β literally just std::filesystem::temp_directory_path, that's the whole name, she said "my name is my name" periodt π
β¨ Synopsis β¨
so this is how she shows up in the wild, defined in header <filesystem>:
path temp_directory_path(); (1) (since C++17)
path temp_directory_path( std::error_code& ec ); (2) (since C++17)
and what does she do? she returns the directory location suitable for temporary files. that's it. that's the whole job. iconic. π
β¨ Parameters β¨
- (none) β she takes nothing, she needs nothing, low-maintenance queen π¦
β¨ Return value β¨
A directory suitable for temporary files π the path is guaranteed to exist and to be a directory (yes, for real, that's the guarantee, no take-backs).
The overload that takes the error_code& argument returns an empty path on error. so like, if things go sideways, you get nothing back instead of an explosion. main character restraint.
β¨ Exceptions β¨
heads up bestie, this part matters π any overload not marked noexcept may throw std::bad_alloc if memory allocation fails. like, sorry, the vibes needed RAM.
- Throws
std::filesystem::filesystem_erroron underlying OS API errors, constructed with the path to be returned as the first path argument and the OS error code as the error code argument. she's very specific about her receipts π - Sets a
std::error_code¶meter to the OS API error code if an OS API call fails, and executesec.clear()if no errors occur. so the no-throw overload cleans up after itself, we love a tidy queen π§Ή
β¨ Notes β¨
okay so this is the tea β
On POSIX systems, the path may be the one specified in the environment variables TMPDIR, TMP, TEMP, TEMPDIR, and, if none of them are specified, the path "/tmp" is returned. she checks all four, in that order, and then falls back to the classic /tmp like the reliable bestie she is π«Ά
On Windows systems, the path is typically the one returned by GetTempPath. different OS, different vibe, same energy β¨
β¨ Example β¨
// Run this code
#include <filesystem>
#include <iostream>
namespace fs = std::filesystem;
int main()
{
std::cout << "Temp directory is " << fs::temp_directory_path() << '\n';
}
β¨ Possible output β¨
Temp directory is "C:\Windows\TEMP\"
see? she told you where the vibes live ππ
β¨ See also β¨
tmpfileβ creates and opens a temporary, auto-removing file (function) πcurrent_pathβ returns or sets the current working directory (C++17) (function) π¦
and that's the whole page babes π fr fr, no filler, only facts. she was brought to you by http://cppreference.com, dated 2024.06.10, and yes the manual name up top really does say C++ Standard Libary β we love a typo that's been living there rent free πβ go touch some temp files, ily, bye ππ
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms