C. Create a temporary file mkstemp function.

Source: Internet
Author: User
Int mkstemp (char * template );
The mkstemp function creates and opens a file with a unique file name in the system. Only the current user can access this temporary file and perform read and write operations. The mkstemp function has only one parameter. this parameter is a non-null string ending with "xxxxxx. The mkstemp function replaces "xxxxxx" with a random string to ensure the uniqueness of the file name.
The function returns a file descriptor. If the execution fails,-1 is returned.
In glibc 2.0.6 and earlier glibc libraries, the access permission for this file is 0666, and in libraries later than glibc 2.0.7, the access permission for this file is 0600.
The temporary files should be deleted in time after use; otherwise, the Directory of the temporary files will be full of garbage. The temporary files created by the mkstemp function cannot be automatically deleted. Therefore, after the mkstemp function is executed, you must call the unlink function. The unlink function deletes the directory entry of the file, however, temporary files can also be accessed through file descriptors until the last open process closes the file operator, or the temporary files are automatically and permanently deleted after the program exits.
The following is an example of using mkstemp:
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 exit this program, the temp file will be removed .*/
Close (FD );
}
 

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.