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