一、背景
有時程序需要存儲很大量的數據,或者在幾個進程間交換數據,這時您可能考慮到使用臨時文件。使用臨時文件要考慮幾個問題:
1、保證臨時文件間的文件名不互助沖突。
2、保證臨時文件中內容不被其他用戶或者黑客偷看、刪除和修改。
在linux下有專門處理臨時文件的函數,先簡單接收兩個函數。
二、簡單實用的兩個函數:mkstemp和tempfile
2.1 mkstemp函數
頭文件:#include <stdlib.h>
函數原型:int mkstemp(char * template);
執行:mkstemp函數在系統中以唯一的文件名創建一個文件並打開,而且只有當前用戶才能訪問這個臨時文件,並進行讀、寫操作。mkstemp函數只有一個參數,這個參數是個以“XXXXXX”結尾的非空字符串。mkstemp函數會用隨機產生的字符串替換“XXXXXX”,保證了文件名的唯一性。
結果: 函數返回一個文件描述符,如果執行失敗返回-1。在glibc 2.0.6 以及更早的glibc庫中這個文件的訪問權限是0666,glibc 2.0.7以后的庫這個文件的訪問權限是0600。
備注:臨時文件使用完成后應及時刪除,否則臨時文件目錄會塞滿垃圾。由於mkstemp函數創建的臨時文件不能自動刪除,所以執行完mkstemp函數后要調用unlink函數,unlink函數刪除文件的目錄入口,但臨時文件還可以通過文件描述符進行訪問,直到最后一個打開的進程關閉文件操作符,或者程序退出后臨時文件被自動徹底地刪除。
1 #include <stdio.h> 2 #include <string.h> 3 #include <unistd.h> 4 #include <stdlib.h> 5 6 int write_temp_file(char* buffer,size_t length) { 7 int len=length; 8 char filename_template[]="/tmp/temp_file.XXXXXX"; 9 int fd=mkstemp(filename_template); 10 unlink(filename_template);//Unlink the file, so it'll be removed when close 11 printf("Template file name:%s\n",filename_template); 12 write(fd,&len,sizeof(len)); 13 write(fd,buffer,len); 14 return fd; 15 } 16 17 char* read_temp_file(int fd, size_t* length) { 18 char* buffer; 19 lseek(fd,0,SEEK_SET); 20 read(fd,length,sizeof(size_t)); 21 buffer=(char*)malloc(*length); 22 read(fd,buffer,*length); 23 close(fd); // Temp file will be deleted 24 return buffer; 25 } 26 27 int main(int argc, char** argv) { 28 char buffer[]="Test template files"; 29 int fd=write_temp_file(buffer,strlen(buffer)); 30 int len=0; 31 char* result=read_temp_file(fd,&len); 32 printf("Len:%d\nContent:%s\n",len,result); 33 free(result); 34 return 0; 35 }
2.2 tmpfile函數
頭文件:#include <stdio.h>
函數原型:FILE *tmpfile(void);
執行:tmpfile函數創建並打開一個臨時文件(二進制,在 /tmp文件夾中),並且自動執行了unlink。tmpfile函數返回一個文件描述符,如果執行失敗返回NULL。當程序執行了fclose或者退出時,資源被釋放。
結果:如果成功,該函數返回一個流指針創建的臨時文件。如果不能創建文件,則返回NULL。
1 #include <stdio.h> 2 int main() 3 { 4 FILE *tempfp; 5 char line[256]; 6 tempfp=tmpfile(); 7 if(tempfp==NULL) 8 { 9 printf("tmpfile error!\n"); 10 return 1; 11 } 12 printf("Opened a temporary file OK!\n"); 13 fputs("One line of output \n",tempfp); 14 rewind(tempfp); 15 if(fgets(line, sizeof(line),tempfp)==NULL) 16 { 17 printf("fgets error!\n"); 18 return 2; 19 } 20 fputs(line, stdout); 21 return 0; 22 }