Go to main content

man pages section 3: Basic Library Functions

Exit Print View

Updated: Friday, August 13, 2021



tmpfile, tmpfile_s - create a temporary file


#include <stdio.h>

FILE *tmpfile(void);
#define __STDC_WANT_LIB_EXT1__ 1
#include <stdio.h>

errno_t tmpfile_s(FILE *restrict *restrict streamptr);


The tmpfile() function creates a temporary file in /var/tmp and opens a corresponding stream. If that directory is not accessible, /tmp is used. The file will automatically be deleted when all references to the file are closed. The file is opened as in fopen(3C) for update (wb+).

The largest value that can be represented correctly in an object of type off_t will be established as the offset maximum in the open file description.

The tmpfile_s() function is part of the C11 bounds checking interfaces specified in the C11 standard, Annex K. It is similar to the tmpfile() function with differing parameters and return types in order to provide additional safety checks on the parameters passed. See runtime_constraint_handler(3C) and INCITS/ISO/IEC 9899:2011.

A NULL streamptr value as an argument to tmpfile_s() generates a runtime-constraint violation (runtime_constraint_handler(3C)) and tmpfile_s() will fail to create a file.

Return Values

Upon successful completion, the tmpfile() function returns a pointer to the stream of the file that is created. Otherwise, it returns a null pointer and sets errno to indicate the error.

If the file is created, the tmpfile_s() function returns 0, otherwise, a non-zero value is returned.


The tmpfile() function will fail if:


A signal was caught during the execution of tmpfile().


There are OPEN_MAX file descriptors currently open in the calling process.


The maximum allowable number of files is currently open in the system.


The directory or file system which would contain the new file cannot be expanded.

The tmpfile() function will fail if:


There are FOPEN_MAX streams currently open in the calling process.


Insufficient storage space is available.

The tmpfile_s() function will fail if:


NULL pointer passed.


The stream refers to a file which is unlinked. If the process is killed in the period between file creation and unlinking, a permanent file may be left behind.

The tmpfile() function has a transitional interface for 64-bit file offsets. For more information, see the lf64(7) man page.


See attributes(7) for descriptions of the following attributes:

Interface Stability
See below

The tmpfile() function can be used safely in multithreaded applications. However, the file descriptors may be leaked if a function in the fork() or exec() family is called in another thread before the fcntl(2) function is called to set the FD_CLOEXEC flag. Applications can avoid this leak by instead using the mkostemp(3C) function with the O_CLOEXEC flag, and then using the fdopen(3C) function to associate a stream with the resulting file descriptor.

The tmpfile_s() function cannot be used safely in a multithreaded application due to the runtime constraint handler. For more information, see the runtime_constraint_handler(3C) man page.

See Also

fcntl(2), unlink(2), fdopen(3C), fopen(3C), fopen_s(3C), mkostemp(3C), mkstemp(3C), mktemp(3C), tmpnam(3C), attributes(7), lf64(7), standards(7), runtime_constraint_handler(3C)