std::tmpfile(3)
πΈ std::tmpfile(3) β C++ Standard Libary πΈ
okay bestie, welcome to the std::tmpfile(3) page, straight out of the C++ Standard Libary (yes that's how the header spells it, we don't fix, we respect π
). This is the function that just makes a temp file for you, no questions asked, no filename ideas required. Let's get into it π«Ά
β¨ NAME β¨
std::tmpfile β literally std::tmpfile, that's the whole name, we love a self-titled icon π
β¨ Synopsis β¨
Defined in header <cstdio>:
std::FILE* tmpfile();
It creates and opens a temporary file with a unique auto-generated filename. Yes, you get a file and it names itself. main character energy fr fr π«
The file is opened as a binary file for update (as by std::fopen with access mode "wb+"). At least TMP_MAX files may be opened during the lifetime of a program β quick heads up though, that limit may be shared with std::tmpnam and may be further limited by FOPEN_MAX. So like, don't go feral with it π
If the program closes the file, e.g. by executing std::fclose, the file is automatically deleted. Cleanup crew, no notes π
If the program terminates normally (by calling std::exit, returning from main, etc), all files that were opened by calling std::tmpfile are also automatically deleted. Iconic behaviour, honestly π
If the program terminates abnormally, it is implementation-defined if these temporary files are deleted. so like... maybe? maybe not? the vibes are unspecified, don't count on it π
β¨ Parameters β¨
(none) β she takes nothing, she gives everything π
β¨ Return value β¨
The associated file stream, or a null pointer if an error has occurred. Check it, bestie, always check it π
β¨ Notes β¨
okay this part is genuinely fascinating, listen up π§
On some implementations (e.g. older Linux), this function actually creates, opens, and immediately deletes the file from the file system. The way that works is: as long as an open file descriptor to a deleted file is held by a program, the file exists, but since it was deleted, its name does not appear in any directory, so that no other process can open it. Invisible file. Ghost file. Secret file. Nobody can touch it but you π¦
Once the file descriptor is closed, or once the program terminates (normally or abnormally), the space occupied by the file is reclaimed by the filesystem. The filesystem always wins eventually, periodt β¨
Newer Linux (since 3.11 or later, depending on filesystem) creates such invisible temporary files in one step, via special flag in the open() syscall. Efficiency ate, no cap π₯
On some implementations (e.g. Windows), elevated privileges are required as the function may create the temporary file in a system directory. so highkey you might need admin for this, wild π
β¨ Example β¨
// Run this code
#include <cstdio>
#include <cstdlib>
#include <filesystem>
#include <iostream>
namespace fs = std::filesystem;
int main()
{
std::cout << "TMP_MAX = " << TMP_MAX << '\n'
<< "FOPEN_MAX = " << FOPEN_MAX << '\n';
std::FILE* tmpf = std::tmpfile();
std::fputs("Hello, world", tmpf);
std::rewind(tmpf);
char buf[6];
std::fgets(buf, sizeof buf, tmpf);
std::cout << buf << '\n';
// Linux-specific method to display the tmpfile name
std::cout << fs::read_symlink(
fs::path("/proc/self/fd") /
std::to_string(fileno(tmpf))
) << '\n';
}
β¨ Possible output β¨
TMP_MAX = 238328
FOPEN_MAX = 16
Hello
"/tmp/tmpfBlY1lI (deleted)"
see that (deleted)? that's the ghost file showing its whole self in /proc, we stan a transparent queen π
β¨ See also β¨
tmpnamβ returns a unique filename (function)temp_directory_path(C++17) β returns a directory suitable for temporary files (function)- C documentation for
tmpfile
and that's the page, bestie!! π this little beauty is brought to you by http://cppreference.com and the version/date 2024.06.10, signing off from std::tmpfile(3) π«Άβ¨π
π¦ 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