produce a unique temporary file mkstemp ()

Source: Internet
Author: User

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 ()

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.