tmpfile
Temporary file routines
DescriptionThe tmpfile function returns a pointer to a stream associated with a file descriptor returned by the routine mkstemp. The created file is unlinked before tmpfile returns, causing the file to be automatically deleted when the last reference to it is closed. The file is opened with the access value '<span class="Qlq">w+</span>'. The file is created in the directory determined by the environment variable <span class="Ev">TMPDIR</span> if set. The default location if <span class="Ev">TMPDIR</span> is not set is <span class="Pa">/tmp</span>.
Example 1
#include <stdio.h> int main() { // create a temporary binary file // using the "wb+" access parameters FILE *f = tmpfile(); // ...use the temporary file... // close and delete the temporary file fclose(f); return 0; }The tmpnam function returns a pointer to a file name, in the <span class="Dv">P_tmpdir</span> directory, which did not reference an existing file at some indeterminate point in the past. <span class="Dv">P_tmpdir</span> is defined in the include file #include <stdio.h> If the argument \c str is non- <span class="Dv">NULL</span>, the file name is copied to the buffer it references. Otherwise, the file name is copied to a static buffer. In either case, tmpnam returns a pointer to the file name. \n \n The buffer referenced by \c str is expected to be at least <span class="Dv">L_tmpnam</span> bytes in length. <span class="Dv">L_tmpnam</span> is defined in the include file #include <stdio.h> \n \n The tempnam function is similar to tmpnam , but provides the ability to specify the directory which will contain the temporary file and the file name prefix. \n \n The environment variable <span class="Ev">TMPDIR</span> (if set), the argument \c tmpdir (if non- <span class="Dv">NULL</span>), the directory <span class="Dv">P_tmpdir</span>, and the directory <span class="Pa">/tmp</span> are tried, in the listed order, as directories in which to store the temporary file. \n \n The argument \c prefix, if non- <span class="Dv">NULL</span>, is used to specify a file name prefix, which will be the first part of the created file name. The tempnam function allocates memory in which to store the file name; the returned pointer may be used as a subsequent argument to free.
Return ValuesThe tmpfile function returns a pointer to an open file stream on success, and a <span class="Dv">NULL</span> pointer on error. \n \n The tmpnam and tempfile functions return a pointer to a file name on success, and a <span class="Dv">NULL</span> pointer on error.
ErrorsThe tmpfile function may fail and set the global variable <span class="Va">errno</span> for any of the errors specified for the library functions reference:malloc or mktemp.
Security ConsiderationsT be used in place of these functions. (See the FSA.)
CompatibilityThese interfaces are provided from System V and <span class="Tn">ANSI</span> compatibility only. \n \n Most historic implementations of these functions provide only a limited number of possible temporary file names (usually 26) before file names will start being recycled. System V implementations of these functions (and of mktemp) use the
accesssystem call to determine whether or not the temporary file may be created. This has obvious ramifications for setuid or setgid programs, complicating the portable use of these interfaces in such programs. \n \n The tmpfile interface should not be used in software expected to be used on other systems if there is any possibility that the user does not wish the temporary file to be publicly readable and writable.