There are many, mktemp, tmpfile, and so on that create temporary methods (functions) under Inux. Today only recommended one of the safest and best use: mkstemp.
mkstemp (Create a unique temporary file)
Header files: #include <stdlib.h>
statement:int mkstemp (char * Template)
Return value: Success returns 0, failure returns-1 .
Description: Creates a unique temporary file name, which must be declared as an array rather than as a pointer. the template format is: template. XXXXXX. The last 6 bits must be XXXXXX, and the prefixes are arbitrary.
The 1.mkstemp function creates a file in the system with a unique filename and opens it, and only the current user can access the temporary file.
The 2.mkstemp function has only one parameter, which is a non-empty string ending with "XXXXXX". The MKSTEMP function replaces the "XXXXXX" with a randomly generated string, guaranteeing the uniqueness of the file name
3. Since the temporary file created by the Mkstemp function cannot be deleted automatically, the unlink function is called after the Mkstemp function is executed, and the unlink function deletes the directory entry of the file.
Char temp_template[] = "/TMP/HTP. XXXXXX ";
TFD = Mkstemp (temp_template);
if (! ( TFP = Fdopen (TfD, "w"))) {
fprintf (stderr, "Could not open temp file.\n");
Exit (1);
}
#include <stdio.h> #include <stdlib.h>int main (void) {int Fd;char temp_file[]= "tmp_xxxxxx";/*creat a temp File.*/if ((Fd=mkstemp (temp_file)) ==-1) {printf ("creat temp file faile./n"); exit (1);} /*unlink the Temp file.*/unlink (temp_file);/*then you can read or write the temp file.*///add YOUR code;/*close temp file, When you exit this program, the temp file would be removed.*/close (FD);}
produce a unique temporary file mkstemp ()