Moved to: http://www.wypblog.com/archives/172
A temporary file is a file temporarily used to store data. If you use the method of creating a common file to create a file, you may encounter the problem of whether the file exists and whether the file has the read and write permissions. The following describes how to create a unique temporary file in Linux:
# Include <stdio. h> char * tmpnam (char * s); file * tmpfile ();
The tmpnam () function generates a unique I file name. If the parameter is null, a file name is generated in an internal buffer. When this function is called one time, the file name may be overwritten. If the parameter is not null, the name is copied to the string. The length of the string must be at least l_tmpnam, which is defined in stdio. h. If it fails, the function returns NULL. The tmpfile () function returns the descriptor of a temporary file. The open attribute of the file is read and write. It is equivalent to using fopen to open with W +. If it fails, null is returned.
The following is a short column.
# Include <stdio. h> int main () {char tmpname [l_tmpnam]; char * filename; file * FP; filename = tmpname (tmpname ); printf ("temporary file name is % s \ n", filename); FP = tmpfile (); If (FP) {printf ("temporary file opened! \ N ");} else {perror (" tmpfile ");} return 0 ;}
In addition, there are two functions that can also create temporary files:
# Include <stdio. h> char * mktemp (char * template); int mkstemp (char * template );
The mktemp () function creates a unique temporary file name from the given module template. The template can be the prefix of the file path. The last six characters of the template must be "xxxxxx". For example:
Char template [l_tmpnam]; char * filename; strcpy (template, "/temp/wypxxxxxx"); filename = mktemp (Template ); printf ("temporary file name is % s \ n", filename );
The mkstemp () function is similar to tmpfile (), but the opened file is equivalent to opening the file using the underlying file operation function open.
Reprinted Please note: Reprinted from past memories (http://www.wypblog.com /)
Link: creating temporary files in Linux (http://www.wypblog.com/archives/172)