How to use C language to operate temporary files under Linux
/ ************************************************** ******************
* This article is first in BBS.Bluegem.org CWORLD zone
* I email: chenfei@sohu.com
* If you reprinate this article, please keep the starting ground and your contact method for easy communication, thank you!
*********************************************************** **************** /
Sometimes the program needs to store a lot of data, or exchange data between several processes, then you may take into account the use of temporary files. Use temporary documents to consider a few questions: 1, ensure that the file name between the temporary file is not mutual conflict. 2. Ensure that the content in the temporary file is not smashed, deleted, and modified by other users or hackers. So in Linux, a function MKSTEMP function MKSTEMP function is created in the system with a unique file name, and only the current user has access to this temporary file, the current user can Open and read, write. The MKSTEMP function has only one parameter, which is a non-empty string ending with "xxxxxx". The MKSTEMP function replaces "XXXXXX" with a random-generated string to ensure the uniqueness of the file name. The function returns a file descriptor if the execution fails returns -1. Access to this file in Glibc 2.0.6 and the earlier Glibc library is 0666, and the access permission of the file after GLIBC 2.0.7 is 0600. When the temporary file completes her mission If it does not clear it, it has exited it before the interim file is cleared, and the interim file is located. Since the temporary file created by the MKSTEMP function cannot be automatically deleted (see the TMPFile function below). After performing the MKSTEMP function, call the unlink function, the Unlink function deletes the file entry, so the temporary file can also be accessed through the file descriptor until the last open process turns off the file operator, or the program exits the temporary file is automatically complete. Delete. Routines: Directly use the ADVANCED Linux Programming routines, just translate the comment
#include
After performing read_temp_file function temporary file is removed. * / Char * read_temp_file (temp_file_handle temp_file, size_t * length) {char * buffer; / * The TEMP_FILE handle is a file descriptor to the temporary file * / / * fd is to visit temporary. File Descriptor * / INT FD = Temp_File; / * rebind to the beginning of the file. * / / * Point the file pointer to the beginning * / lseek (fd, 0, seek_set); / * read the size of the Data in the temporary file. * / / * Get a temporary file body * / read (fd, length, sizeof (* length)); / * allocate a buffer and read the data. * / / * Assign the memory block, read Data * / buffer = (char *) Malloc (* length); read (fd, buffer, * length); / * Close The File Descriptor, Which Will Cause The Temporary File To Go Away. * / / * Close the file descriptor The temporary file is completely deleted * / close (fd); return buffer;} TMPFile function If you use the C library I / O function, and no other program use this temporary file (author Note: Press my understanding is the same Process or in a process group with a parent-child relationship, there is a more concise function - TMPFile. The TMPFile function creates and opens a temporary file and automatically executes this temporary file. The TMPFile function returns a file descriptor if the execution failed returns NULL. The resource is released when the program executes FCLOSE or exits. MKTEMP, TMPNAM, and TEMPNAM, etc., but it is not recommended to use them due to robust and security. Reference: Advanced Linux Programming P27 ~ 29Linux Programing Unleashed P171 ~ 172MAN Page