What Linux environment variable stores the path to the temp \ tmp folder? I need to programmatically find the path to this folder. C ++ program.
- Not the fact that in general in any. I, for example, do not have such a variable under Mint and Debian. - PinkTux
- Specify whether you need to find out the path to the temp folder in the system (in this case, you need to specify what criteria to search for - for example, this folder should be specified in some variable), or programmatically create a temporary file in the folder intended for temporary files, and in which this file is guaranteed to be created? IMHO, it will depend on this, what answer you get. - lospejos
- if the memory does not change then the set command will show you everything - perfect
4 answers
according to the posix standard, the directory for storing temporary files is the /tmp .
but since If you ask specifically about the gnu / linux operating system, then the fhs standard is applicable, according to which the place to save temporary files is not only the /tmp , but also the /var/tmp . the difference between them is that the first is usually (but not necessarily) cleared when the operating system boots.
but since you also tmpfile() about c ++ , then the above information can be neglected due to the presence of the tmpfile() function tmpfile() standard and its c ++ analog std :: tmpfile () .
the presence of any environment variables associated with the directory for storing temporary files, as far as I know, is not required by any of these standards.
If defined, then TMPDIR .
Otherwise use the default /tmp
- Is that enough to refer to / tmp? - Sergey
- @ Sergey, for what? In order to create a temporary file / directory, for example, no information about the environment or the path is needed. For man tempfile and similarly for software use. - PinkTux
- @Sergey, the location of the
\tmp(as well as a number of other folders) is fixed in the Filesystem Hierarchy Standard . - ߊߚߤߘ - @Sergey, as noted above - it’s not clear what you need to access the temporary folder =) Also, the
/var/tmpcan also be used. - Nikolay
First, it makes sense to check the TMPDIR environment TMPDIR . If it exists and is the way - then use it.
In win32 api look at the GetTempPathW method. (mentioned for completeness)
If it does not exist, then the standard stdio.h header may declare P_tmpdir .
If all else fails, you can use the standard /tmp , but only as a last resort.
PS: the answer is based on the implementation of the sys_get_temp_dir function in php .
Found a function that allows you to take the path to the folder c temporary files in both Windows and Linux.
std::experimental::filesystem::temp_directory_path();