How to use C language to operate temporary files under Linux

xiaoxiao2021-03-06  43

This article is first in Telnet bbs.bluegem.org my email: Chenfei@sohu.com If you reprinted this article, please keep the starting ground and your contact method for easy communication, thank you! This is my work notes, I hope you have a big cattle, and the old man has heard! How to use C language under Linux Temporary file 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 #include / * a Handle for a Temporary File Created with write_temp_file. In this Implementation, IT's Just A . file descriptor * / / * write_temp_file is a handle for a temporary file, in this case just a file descriptor * / typedef int temp_file_handle; / * Writes LENGTH bytes from BUFFER into a temporary file the temporary file is immediately unlinked Returns a.. Handle to the temporary file. * / / * Write the length data from the temporary file from the buffer. The temporary file is removed in just one creation. The function returns the handle of the temporary file.

* / Temp_file_handle write_temp_file (char * buffer, size_t length) {/ * Create the filename and file. The XXXXXX will be replaced with characters that make the filename unique. * / / * Create a new file name and file, the file name is XXXXXX will be Random string instead to ensure the uniqueness of the file name in the system * / char TEMP_FILENAME [] = "/TMP/TEMP_FILE.XXXXXX"; int fd = mkstemp (Temp_FileName); / * unlink the file immediately, so that it will Will BE Removed. * / / * The file immediately by Unlink, so as long as the file descriptor closed the file, it will be automatically deleted * / unlink (TEMP_FILENAME); / * Write the number of bytes to the file first. * / / * First, write upcoming the length of the data is about * / write (fd, & length, sizeof (length)); / * now write the data itself. * / / * Write data itself * / write (fd, buffer , length); / * Use the file descriptor as the handle for the temporary file. * / / * function Returns the file descriptor, the handle of the temporary file * / return fd;} / * reads the contents of a temporary File Temp_file created WITH WRITE_TEMP_FILE. THE RETURN VALUE IS A NEWLY Allocated Buffer of Those Contents, Which The Caller Must Deallocate with free. * Length is set to the size of the contents, in bytes. th e Temporary File Is Removed. * / / * reads data from the temporary file created by Write_Temp_File. The return value is a newly requested memory block containing the file content, which should also call read_temp_files. * Length is the length of the textual content of the temporary file.

转载请注明原文地址:https://www.9cbs.com/read-55711.html

New Post(0)