Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use mkstemp command to operate temporary files in CentOS

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

CentOS how to use the mkstemp command to operate temporary files, many novices are not very clear about this, in order to help you solve this problem, the following small series will explain in detail for everyone, there are people who need this can learn, I hope you can harvest.

mkstemp function

int mkstemp(char *template);

The mkstemp function creates and opens a file with a unique file name in the system, and only the current user can access the temporary file for read and write operations. The mkstemp function takes only one argument, which is a non-empty string ending in "XXXXXX." The mkstemp function replaces "XXXXXX" with a randomly generated string, ensuring the uniqueness of the file name. Function returns a file descriptor or-1 if execution fails. Access to this file is 0666 in glibc 2.0.6 and earlier libraries, and 0600 in libraries after glibc 2.0.7.

Temporary files should be deleted in time after use, otherwise the temporary file directory will be full of garbage. Since temporary files created by mkstemp function cannot be deleted automatically, unlink function is called after mkstemp function is executed. Unlink function deletes directory entry of files, but temporary files can also be accessed through file descriptor until the last open process closes file operator, or temporary files are deleted automatically and completely after program exit.

[cpp]view plaincopy View Code Pieces on CODE Derive to My Code Pieces #include #include #include #include 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); write(fd,&len,sizeof(len)); write(fd,buffer,len); return fd; } char* read_temp_file(int fd, size_t* length) { char* buffer; lseek(fd,0,SEEK_SET); read(fd,length,sizeof(size_t)); buffer=(char*)malloc(*length); read(fd,buffer,*length); 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); printf("Len:%d\nContent:%s\n",len,result); free(result); return 0; }

tmpfile function

If you use the C library I/O function and there is no other program that uses the temporary file, there is a simpler function--tmpfile. The tmpfile function creates and opens a temporary file and automatically executes unlink. The tmpfile function returns a file descriptor and NULL if it fails. When the program executes fclose or exits, resources are released.

In addition, linux systems also provide mktemp, tmpnam, and tempnam functions, but due to robustness and security issues, not recommended.

Did reading the above help you? If you still want to have further understanding of related knowledge or read more related articles, please pay attention to the industry information channel, thank you for your support.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report