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

# 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 to the temporary file for operations. In this example, it is only a file descriptor */

Typedef int temp_file_handle;

/* Writes LENGTH bytes from BUFFER into a temporary file.

Temporary file is immediately unlinked. Returns a handle to

Temporary file .*/

/* Write the LENGTH byte data from the BUFFER to the temporary file. The temporary file is deleted after it is created. 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

Characters that make the filename unique .*/

/* Create a new file name and file. XXXXXX in the file name will be replaced by a random string 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 be removed when

File descriptor is closed .*/

/* The file is unlink immediately, so that the file will be automatically deleted as soon as the file descriptor is closed */

Unlink (temp_filename );

/* Write the number of bytes to the file first .*/

/* First write the length of the data to be written */

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 .*/

/* The function returns the file descriptor as the handle to the temporary file */

Return fd;

}

/* Reads the contents of a temporary file TEMP_FILE created

Write_temp_file. The return value is a newly allocated buffer

Those contents, which the caller must deallocate with free.

* LENGTH is set to the size of the contents, in bytes.

Temporary file is removed .*/

/* Read data from the temporary file created by write_temp_file. The returned value is the newly applied memory block containing the file content. This memory should be released by calling read_temp_file again.

* Length is the length of the body of the temporary file. The temporary file is permanently deleted after the read_temp_file function is executed */

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 the file descriptor used to access temporary files */

Int fd = temp_file;

/* Rewind to the beginning of the file .*/

/* Point the file pointer to the beginning of the file */

Lseek (fd, 0, SEEK_SET );

/* Read the size of the data in the temporary file .*/

/* Get the body length of the temporary file */

Read (fd, length, sizeof (* length ));

/* Allocate a buffer and read the data .*/

/* Allocate memory blocks to read data */

Buffer = (char *) malloc (* length );

Read (fd, buffer, * length );

/* Close the file descriptor, which will cause the temporary file

Go away .*/

/* Close the file descriptor and delete the temporary file */

Close (fd );

Return buffer;

}


Tmpfile Function


If you use the C library I/O function and no other program uses this temporary file (note: in my understanding, it is in the same process or a parent-child process group.) There is a more concise function called tmpfile. The tmpfile function creates and opens a temporary file, and automatically runs the unlink temporary file. The tmpfile function returns a file descriptor. If execution fails, NULL is returned. When the program executes fclose or exits, the resource is released.

Mktemp, tmpnam, and tempnam functions are also provided in linux, but they are not recommended for robustness and security reasons.

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.