GSP
Quick Navigator

Search Site

Unix VPS
A - Starter
B - Basic
C - Preferred
D - Commercial
MPS - Dedicated
Previous VPSs
* Sign Up! *

Support
Contact Us
Online Help
Handbooks
Domain Status
Man Pages

FAQ
Virtual Servers
Pricing
Billing
Technical

Network
Facilities
Connectivity
Topology Map

Miscellaneous
Server Agreement
Year 2038
Credits
 

USA Flag

 

 

Man Pages
std::tmpfile(3) C++ Standard Libary std::tmpfile(3)

std::tmpfile - std::tmpfile


Defined in header <cstdio>
std::FILE* tmpfile();


Creates and opens a temporary file with a unique auto-generated filename.


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 (this
limit may be shared with std::tmpnam and may be further limited by FOPEN_MAX).


If the program closes the file, e.g. by executing std::fclose, the file is
automatically deleted.


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.


If the program terminates abnormally, it is implementation-defined if these
temporary files are deleted.


(none)


The associated file stream or a null pointer if an error has occurred


On some implementations (e.g. older Linux), this function actually creates, opens,
and immediately deletes the file from the file system: 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. 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. 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.


On some implementations (e.g. Windows), elevated privileges are required as the
function may create the temporary file in a system directory.

// Run this code


#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <filesystem>
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';
}


TMP_MAX = 238328
FOPEN_MAX = 16
Hello
"/tmp/tmpfBlY1lI (deleted)"


tmpnam returns a unique filename
(function)
temp_directory_path returns a directory suitable for temporary files
(C++17) (function)

2022.07.31 http://cppreference.com

Search for    or go to Top of page |  Section 3 |  Main Index

Powered by GSP Visit the GSP FreeBSD Man Page Interface.
Output converted with ManDoc.