Linux temporary files

Source: Internet
Author: User

To use temporary files, consider the following:

  1. Ensure that the file 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.

The mkstemp and tmpfile functions are provided in Linux to process temporary files.

Mkstemp Function

int mkstemp(char *template);

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

The temporary files should be deleted in time after use; otherwise, the Directory of the temporary files will be full of garbage. The temporary files created by the mkstemp function cannot be automatically deleted. Therefore, after the mkstemp function is executed, you must call the unlink function. The unlink function deletes the directory entry of the file, however, temporary files can be accessed through file descriptors until the last opened process closes the file operator, or the temporary files are automatically and permanently deleted after the program exits.

#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>

int write_temp_file(char* buffer,size_t length) {
int len=length;
char filename_template[]="/tmp/temp_file.XXXXXX";
int fd=mkstemp(filename_template);
unlink(filename_template);//Unlink the file, so it'll be removed when close
printf("Template file name:%s\n",filename_template);
return fd;

char* read_temp_file(int fd, size_t* length) {
char* buffer;
close(fd); // Temp file will be deleted
return buffer;

int main(int argc, char** argv) {
char buffer[]="Test template files";
int fd=write_temp_file(buffer,strlen(buffer));
int len=0;
char* result=read_temp_file(fd,&len);
return 0;

Tmpfile Function

If you use the C library I/O function and no other program uses this temporary file, there is a more concise function-tmpfile. The tmpfile function creates and opens a temporary file and automatically runs Unlink. The tmpfile function returns a file descriptor. If execution fails, null is returned. When the program executes fclose or exits, the resource is released.

In addition, mktemp, tmpnam, tempnam, and other functions are provided in Linux. However, due to robustness and security issues, it is not recommended.

Related Article

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: 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.