1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
|
<h1 id="firstHeading" class="firstHeading">tmpfile, tmpfile_s</h1> <table class="t-dcl-begin"> <tr class="t-dsc-header"> <th> Defined in header <code><stdio.h></code> </th> <th> </th> <th> </th> </tr> <tr class="t-dcl"> <td> <pre data-language="c">FILE* tmpfile( void );</pre>
</td> <td> (1) </td> <td class="t-dcl-nopad"> </td> </tr> <tr class="t-dcl t-since-c11"> <td> <pre data-language="c">errno_t tmpfile_s( FILE* restrict* restrict streamptr );</pre>
</td> <td> (2) </td> <td> <span class="t-mark-rev t-since-c11">(since C11)</span> </td> </tr> </table> <div class="t-li1">
<span class="t-li">1)</span> Creates and opens a temporary file. The file is opened as binary file for update (as if by <code><a href="fopen" title="c/io/fopen">fopen</a></code> with <span class="st0">"wb+"</span> mode). The filename of the file is guaranteed to be unique within the filesystem. At least <code><a href="../io" title="c/io">TMP_MAX</a></code> files may be opened during the lifetime of a program (this limit may be shared with <code><a href="tmpnam" title="c/io/tmpnam">tmpnam</a></code> and may be further limited by <code><a href="../io" title="c/io">FOPEN_MAX</a></code>).</div> <div class="t-li1">
<span class="t-li">2)</span> Same as <span class="t-v">(1)</span>, except that at least <code>TMP_MAX_S</code> files may be opened (the limit may be shared with <code>tmpnam_s</code>), and if <code>streamptr</code> is a null pointer, the currently installed <a href="../error/set_constraint_handler_s" title="c/error/set constraint handler s">constraint handler</a> function is called. As with all bounds-checked functions, <code>tmpfile_s</code> only guaranteed to be available if <code>__STDC_LIB_EXT1__</code> is defined by the implementation and if the user defines <code>__STDC_WANT_LIB_EXT1__</code> to the integer constant <code>1</code> before including <a href="../io" title="c/io"><code><stdio.h></code></a>.</div> <p>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.</p>
<h3 id="Parameters"> Parameters</h3> <div class="t-li1">
<span class="t-li">1)</span> (none)</div> <div class="t-li1">
<span class="t-li">2)</span> pointer to a pointer that will be updated by this function call</div> <h3 id="Return_value"> Return value</h3> <div class="t-li1">
<span class="t-li">1)</span> Pointer to the file stream associated with the file or null pointer if an error has occurred.</div> <div class="t-li1">
<span class="t-li">2)</span> Zero if the file was created and open successfully, non-zero if the file was not created or open or if <code>streamptr</code> was a null pointer. In addition, pointer to the associated file stream is stored in <code>*streamptr</code> on success, and a null pointer value is stored in <code>*streamptr</code> on error.</div> <h3 id="Notes"> Notes</h3> <p>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 <a rel="nofollow" class="external text" href="https://man7.org/linux/man-pages/man2/open.2.html"><code>open()</code></a> syscall.</p>
<p>On some implementations (e.g. Windows), elevated privileges are required as the function may create the temporary file in a system directory.</p>
<h3 id="Example"> Example</h3> <div class="t-example"> <div class="c source-c"><pre data-language="c">#define _POSIX_C_SOURCE 200112L
#include <stdio.h>
#include <unistd.h>
int main(void)
{
printf("TMP_MAX = %d, FOPEN_MAX = %d\n", TMP_MAX, FOPEN_MAX);
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
char fname[FILENAME_MAX], link[FILENAME_MAX] = {0};
sprintf(fname, "/proc/self/fd/%d", fileno(tmpf));
if (readlink(fname, link, sizeof link - 1) > 0)
printf("File name: %s\n", link);
}</pre></div> <p>Possible output:</p>
<div class="text source-text"><pre data-language="c">TMP_MAX = 238328, FOPEN_MAX = 16
got back from the file: 'Hello'
File name: /tmp/tmpfjptPe5 (deleted)</pre></div> </div> <h3 id="References"> References</h3> <ul>
<li> C23 standard (ISO/IEC 9899:2023): </li>
<ul>
<li> 7.21.4.3 The tmpfile function (p: TBD) </li>
<li> K.3.5.1.1 The tmpfile_s function (p: TBD) </li>
</ul>
<li> C17 standard (ISO/IEC 9899:2018): </li>
<ul>
<li> 7.21.4.3 The tmpfile function (p: 222) </li>
<li> K.3.5.1.1 The tmpfile_s function (p: 427) </li>
</ul>
<li> C11 standard (ISO/IEC 9899:2011): </li>
<ul>
<li> 7.21.4.3 The tmpfile function (p: 303) </li>
<li> K.3.5.1.1 The tmpfile_s function (p: 586-587) </li>
</ul>
<li> C99 standard (ISO/IEC 9899:1999): </li>
<ul><li> 7.19.4.3 The tmpfile function (p: 269) </li></ul>
<li> C89/C90 standard (ISO/IEC 9899:1990): </li>
<ul><li> 4.9.4.3 The tmpfile function </li></ul>
</ul> <h3 id="See_also"> See also</h3> <table class="t-dsc-begin"> <tr class="t-dsc"> <td> <div><a href="tmpnam" title="c/io/tmpnam"> <span class="t-lines"><span>tmpnam</span><span>tmpnam_s</span></span></a></div>
<div><span class="t-lines"><span><span class="t-mark-rev t-since-c11">(C11)</span></span></span></div> </td> <td> returns a unique filename <br> <span class="t-mark">(function)</span> </td>
</tr> <tr class="t-dsc"> <td colspan="2"> <span><a href="https://en.cppreference.com/w/cpp/io/c/tmpfile" title="cpp/io/c/tmpfile">C++ documentation</a></span> for <code>tmpfile</code> </td>
</tr> </table> <div class="_attribution">
<p class="_attribution-p">
© cppreference.com<br>Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.<br>
<a href="https://en.cppreference.com/w/c/io/tmpfile" class="_attribution-link">https://en.cppreference.com/w/c/io/tmpfile</a>
</p>
</div>
|