如何在linux下使用c語言操作臨時檔案

來源:互聯網
上載者:User

如何在linux下使用c語言操作臨時檔案

/******************************************************************

*本文首發於bbs.bluegem.org的CWorld區

*本人email:chenfei@sohu.com

*如轉載本文,請保留首發地和本人聯絡方式,以方便交流,謝謝!

******************************************************************/ 

  有時程式需要儲存很大量的資料,或者在幾個進程間交換資料,這時您可能考慮到使用臨時檔案。使用臨時檔案要考慮幾個問題:
1、保證臨時檔案間的檔案名稱不互助衝突。
2、保證臨時檔案中內容不被其他使用者或者駭客偷看、刪除和修改。
所以在linux下有專門處理臨時檔案的函數
mkstemp函數
mkstemp函數將在系統中以獨一無二的檔案名稱建立一個檔案並開啟,而且只有目前使用者才有訪問這個臨時檔案的許可權,目前使用者對這個臨時檔案可以開啟並進行讀、寫操作。mkstemp函數只有一個參數,這個參數是個以“XXXXXX”結尾的非Null 字元串。mkstemp函數會用隨機產生的字串替換“XXXXXX”,保證了檔案名稱的唯一性。函數返回一個檔案描述符,如果執行失敗返回-1。在glibc 2.0.6 以及更早的glibc庫中這個檔案的存取權限是0666 ,glibc 2.0.7 以後的庫這個檔案的存取權限是0600。
當臨時檔案完成她的使命如果不把它清除乾淨把或者程式由於意外在臨時檔案被清除前就已經退出,臨時檔案所在的目錄會塞滿垃圾。由於mkstemp函數建立的臨時檔案不能自動刪除(請參考下文中的tmpfile函數)。執行完mkstemp函數後要調用unlink函數,unlink函數刪除檔案的目錄入口,所以臨時檔案還可以通過檔案描述符進行訪問,直到最後一個開啟的進程關閉檔案操作符,或者程式退出後臨時檔案被自動徹底地刪除。
常式:
直接使用advanced linux programming的常式,只把注釋翻譯一下

#include <stdlib.h>
#include <unistd.h>
/* A handle for a temporary file created with write_temp_file. In
this implementation, it’s just a file descriptor. */
/*write_temp_file是個操作臨時檔案的控制代碼,本例中只是個檔案描述符*/
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. */
/*在這函數從BUFFER中向臨時檔案寫入LENGTH位元組資料。臨時檔案在剛一建立就被刪除掉。函數會返回臨時檔案的控制代碼。*/
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. */
/*建立檔案名稱和檔案,檔案名稱中的XXXXXX將被隨機字串代替,以保證檔案名稱在系統中的唯一性*/
 char temp_filename[] = “/tmp/temp_file.XXXXXX”;
 int fd = mkstemp (temp_filename);
 /* Unlink the file immediately, so that it will be removed when the
 file descriptor is closed. */
 /*檔案立刻被unlink,這樣只要檔案描述符一關閉檔案就會被自動刪除*/
 unlink (temp_filename);
 /* Write the number of bytes to the file first. */
 /*首先寫入即將寫入資料的長度*/
 write (fd, &length, sizeof (length));
 /* Now write the data itself. */
 /*寫入資料本身*/
 write (fd, buffer, length);
 /* Use the file descriptor as the handle for 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. The
temporary file is removed. */
/*從被write_temp_file建立的臨時檔案中讀取資料。傳回值是含有檔案內容的新申請到的記憶體塊,這塊記憶體應該又調用read_temp_file者釋放。
*length是臨時檔案本文內容的長度。執行完read_temp_file函數後臨時檔案被徹底刪除*/
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是訪問臨時檔案的檔案描述符*/
 int fd = temp_file;
 /* Rewind to the beginning of the file. */
 /*把檔案指標指向檔案開頭*/
 lseek (fd, 0, SEEK_SET);
 /* Read the size of the data in the temporary file. */
 /*獲得臨時檔案本文長度*/
 read (fd, length, sizeof (*length));
 /* Allocate a buffer and read the data. */
 /*分配記憶體塊,讀取資料*/
 buffer = (char*) malloc (*length);
 read (fd, buffer, *length);
 /* Close the file descriptor, which will cause the temporary file to
 go away. */
 /*關閉檔案描述符,臨時檔案被徹底刪除*/
 close (fd);
 return buffer;
}

tmpfile函數
如果您使用C library I/O函數,並且並沒有另一個程式使用這個臨時檔案(筆者註:按我的理解是在同一進程或具有父子關係的進程組中),有個更簡潔的函數——tmpfile。tmpfile函數建立並開啟一個臨時檔案,並且自動執行了unlink了這個臨時檔案。tmpfile函數返回一個檔案描述符,如果執行失敗返回NULL。當程式執行了fclose或者退出時,資源被釋放。
linux系統中還提供mktemp、 tmpnam、 和tempnam等函數,但是由於健壯性和安全方面理由不建議使用他們。

參考文獻:
Advanced Linux Programming p27~29
Linux Programing Unleashed p171~172
man page

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.