Use C language to operate temporary files in linux

Source: Internet
Author: User
In linux, use the C language to operate on temporary files-general Linux technology-Linux programming and kernel information. The following is a detailed description. Sometimes a program needs to store a large amount of data or exchange data between several processes, you may consider using temporary files. To use temporary files, consider the following:

1. Ensure that the names of temporary files do not conflict with each other.
2. Ensure that the contents of temporary files are not peeked, deleted, or modified by other users or hackers.

Therefore, in linux, there are functions dedicated to handling temporary files.

Mkstemp Function

The mkstemp function creates and opens a file with a unique file name in the system. Only the current user has the permission to access this temporary file, the current user can open and read and write this temporary file. 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.

When the temporary file completes her mission, if it is not cleared, or the program has exited before it is accidentally cleared, the directory where the temporary file is located will be full of garbage. Temporary files created by the mkstemp function cannot be automatically deleted (refer to the tmpfile function below ). After executing the mkstemp function, you need to call the unlink function. The unlink function deletes the directory entry of the file. Therefore, temporary files can be accessed through the file descriptor until the last opened process closes the file operator, or the temporary files are automatically and permanently deleted after the program exits.

Routine:

Directly use the advanced linux programming routine to translate comments
QUOTE:

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.