tmpfile, tmpfile_s

From cppreference.com
< c‎ | io
 
 
File input/output


Functions
File access
(C11)
(C95)
Direct input/output
Unformatted input/output
(C95)(C95)
(C95)
(C95)(C95)
(C95)
(C95)
(C95)
(C95)
Formatted input
Formatted output
File positioning
Error handling
Operations on files
tmpfiletmpfile_s
(C11)
 
Defined in header <stdio.h>
FILE *tmpfile();
(1)
errno_t tmpfile_s(FILE * restrict * restrict streamptr);
(2) (since C11)
1) Creates and opens a temporary file. The file is opened as binary file for update (as if by fopen with "wb+ mode). The filename of the file is guaranteed to be unique within the filesystem. At least TMP_MAX files may be opened during the lifetime of a program (this limit may be shared with tmpnam and may be further limited by FOPEN_MAX).
2) Same as (1), except that at least TMP_MAX_S files may be opened (the limit may be shared with tmpnam_s), and if streamptr is a null pointer, the currently installed constraint handler function is called.
As all bounds-checked functions, tmpfile_s is only guaranteed to be available of __STDC_LIB_EXT1__ is defined by the implementation and if the user defines __STDC_WANT_LIB_EXT1__ to the integer constant 1 before including <stdio.h>.

The temporary file created by this function is closed and deleted when the program exits normally. Whether it's deleted on abnormal termination is implementation-defined.

Contents

[edit] Parameters

1) (none)
2) pointer to a pointer that will be updated by this function call

[edit] Return value

1) Pointer to the file stream associated with the file or null pointer if an error has occurred.
2) Zero if the file was created and open successfully, non-zero if the file was not created or open or if streampos was a null pointer. In addition, pointer to the associated file stream is stored in streampos on success, and a null pointer value is stored in streampos on error.

[edit] Notes

On some implementations (e.g. 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.

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

[edit] Example

#include <stdio.h>
#include <stdlib.h>
 
int main(void)
{
    FILE* tmpf = tmpfile();
    fputs("Hello, world", tmpf);
    rewind(tmpf);
    char buf[6];
    fgets(buf, sizeof buf, tmpf);
    printf("got back from the file: '%s'\n", buf);
 
    // Linux-specific method to display the tmpfile name
    system("ls -l /proc/self/fd/3");
}

Output:

got back from the file: Hello
lrwx------ 1 user group 64 Jan 22 09:49 /proc/self/fd/3 -> /tmp/tmpfZmrsOR (deleted)

[edit] References

  • C11 standard (ISO/IEC 9899:2011):
  • 7.21.4.3 The tmpfile function (p: 303)
  • K.3.5.1.1 The tmpfile_s function (p: 586-587)
  • C99 standard (ISO/IEC 9899:1999):
  • 7.19.4.3 The tmpfile function (p: 269)
  • C89/C90 standard (ISO/IEC 9899:1990):
  • 4.9.4.3 The tmpfile function

[edit] See also

returns a unique filename
(function)
C++ documentation for tmpfile