<p>Let's say I'm creating a program in C that needs to use a tempfile. Creating an ad hoc tempfile in /tmp is probably not a good idea. Is there a function or OS call to supply me with a tempfile name so that I can begin to write and read from it?</p>

[{'answer_id': 29656, 'author': 'gavrie', 'author_id': 2794, 'author_profile': 'https://Stackoverflow.com/users/2794', 'pm_score': 5, 'selected': True, 'text': '<p>You can use the <a href="http://www.linuxmanpages.com/man3/mkstemp.3.php" rel="noreferrer"><code>mkstemp(3)</code></a> function for this purpose. Another alternative is the <a href="http://www.linuxmanpages.com/man3/tmpfile.3.php" rel="noreferrer"><code>tmpfile(3)</code></a> function.\nWhich one of them you choose depends on whether you want the file to be opened as a C library file stream (which <code>tmpfile</code> does), or a direct file descriptor (<code>mkstemp</code>). The <code>tmpfile</code> function also deletes the file automatically when you program finishes.</p>\n\n<p>The advantage of using these functions is that they avoid race conditions between determining the unique filename and creating the file -- so that two programs won\'t try to create the same file at the same time, for example.</p>\n\n<p>See the man pages for both functions for more details.</p>\n'}, {'answer_id': 29657, 'author': 'DMC', 'author_id': 3148, 'author_profile': 'https://Stackoverflow.com/users/3148', 'pm_score': 2, 'selected': False, 'text': '<p>Absolutely: man mkstemp.</p>\n\n<p>The man page has example usage.</p>\n'}, {'answer_id': 29659, 'author': 'Frep D-Oronge', 'author_id': 3024, 'author_profile': 'https://Stackoverflow.com/users/3024', 'pm_score': 1, 'selected': False, 'text': '<p>Not sure about anything in a C lib, but you can do this at the shell with <a href="http://linux.die.net/man/1/mktemp" rel="nofollow noreferrer">mktemp</a>.</p>\n'}, {'answer_id': 29693, 'author': 'gavrie', 'author_id': 2794, 'author_profile': 'https://Stackoverflow.com/users/2794', 'pm_score': 2, 'selected': False, 'text': "<p>@garethm:</p>\n\n<blockquote>\n  <p>I believe that the function you're looking for is called tmpnam.</p>\n</blockquote>\n\n<p>You should definitely <em>not</em> use <code>tmpnam</code>. It suffers from the race condition problem I mentioned in my answer: Between determining the name and opening it, another program may create the file or a symlink to it, which is a huge security hole.</p>\n\n<p>The <code>tmpnam</code> man page specifically says not to use it, but to use <code>mkstemp</code> or <code>tmpfile</code> instead.</p>\n"}, {'answer_id': 31587, 'author': 'Mark Borgerding', 'author_id': 3343, 'author_profile': 'https://Stackoverflow.com/users/3343', 'pm_score': 3, 'selected': False, 'text': '<p>The question is how to generate a temporary file <strong>name</strong>.  Neither mkstemp nor tmpfile provide the caller with a name, they return a file descriptor or file handle, respectively. </p>\n'}, {'answer_id': 37542952, 'author': 'Cristiano Pereira', 'author_id': 6145218, 'author_profile': 'https://Stackoverflow.com/users/6145218', 'pm_score': 0, 'selected': False, 'text': '<p>You should use the mkstemp() as this is the recommended function, but it returns a file descriptor, so once you have the descriptor get it\'s name:</p>\n\n<pre><code>int fd;\nfd = mkstemp("hdrXXXXXX);\n/* Read out the link to our file descriptor. */\nsprintf(path, "/proc/self/fd/%d", fd);\nmemset(result, 0, sizeof(result));\nreadlink(path, result, sizeof(result)-1);\n\n/* Print the result. */\nprintf("%s\\n", result);\n</code></pre>\n'}, {'answer_id': 64954616, 'author': 'hanshenrik', 'author_id': 1067003, 'author_profile': 'https://Stackoverflow.com/users/1067003', 'pm_score': 0, 'selected': False, 'text': '<p>usually there\'s no need to actually make a named file; instead use the file descriptor path,</p>\n<pre class="lang-c prettyprint-override"><code>FILE *tmp=tmpfile();\nchar path[PATH_MAX+1]={0};\nsprintf(path, &quot;/dev/fd/%d&quot;, fileno(tmp));\nprintf(&quot;%s\\n&quot;, path);\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/29647', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/432/']