在linux系统下使用C语言操作临时文件
有時程序需要存儲很大量的數據,或者在幾個進程間交換數據,這時您可能考慮到使用臨時文件。使用臨時文件要考慮幾個問題:
1、保證臨時文件間的文件名不互助沖突。
2、保證臨時文件中內容不被其他用戶或者黑客偷看、刪除和修改。
所以在linux下有專門處理臨時文件的函數
mkstemp函數
mkstemp函數將在系統中以獨一無二的文件名創建一個文件并打開,而且只有當前用戶才有訪問這個臨時文件的權限,當前用戶對這個臨時文件可以打開并進行讀、寫操作。mkstemp函數只有一個參數,這個參數是個以“XXXXXX”結尾的非空字符串。mkstemp函數會用隨機產生的字符串替換“XXXXXX”,保證了文件名的唯一性。函數返回一個文件描述符,如果執行失敗返回-1。在glibc 2.0.6 以及更早的glibc庫中這個文件的訪問權限是0666 ,glibc 2.0.7 以后的庫這個文件的訪問權限是0600。
當臨時文件完成她的使命如果不把它清除干凈把或者程序由于意外在臨時文件被清除前就已經退出,臨時文件所在的目錄會塞滿垃圾。由于mkstemp函數創建的臨時文件不能自動刪除(請參考下文中的tmpfile函數)。執行完mkstemp函數后要調用unlink函數,unlink函數刪除文件的目錄入口,所以臨時文件還可以通過文件描述符進行訪問,直到最后一個打開的進程關閉文件操作符,或者程序退出后臨時文件被自動徹底地刪除。
例程:
直接使用advanced linux programming的例程,只把注釋翻譯一下
#include#include/* A handle for a temporary file created with write_temp_file. Inthis implementation, it’s just a file descriptor. *//*write_temp_file是個操作臨時文件的句柄,本例中只是個文件描述符*/typedef int temp_file_handle;/* Writes LENGTH bytes from BUFFER into a temporary file. Thetemporary file is immediately unlinked. Returns a handle to thetemporary file. *//*在這函數從BUFFER中向臨時文件寫入LENGTH字節數據。臨時文件在剛一創建就被刪除掉。函數會返回臨時文件的句柄。*/temp_file_handle write_temp_file (char* buffer, size_t length){/* Create the filename and file. The XXXXXX will be replaced withcharacters 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 thefile 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 withwrite_temp_file. The return value is a newly allocated buffer ofthose contents, which the caller must deallocate with free.*LENGTH is set to the size of the contents, in bytes. Thetemporary 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 togo away. *//*關閉文件描述符,臨時文件被徹底刪除*/close (fd);return buffer;}tmpfile函數
如果您使用C library I/O函數,并且并沒有另一個程序使用這個臨時文件(筆者注:按我的理解是在同一進程或具有父子關系的進程組中),有個更簡潔的函數——tmpfile。tmpfile函數創建并打開一個臨時文件,并且自動執行了unlink了這個臨時文件。tmpfile函數返回一個文件描述符,如果執行失敗返回NULL。當程序執行了fclose或者退出時,資源被釋放。
linux系統中還提供mktemp、 tmpnam、 和tempnam等函數,但是由于健壯性和安全方面理由不建議使用他們。
以上是云棲社區小編為您精心準備的的內容,在云棲社區的博客、問答、公眾號、人物、課程等欄目也有 的相關內容,歡迎繼續使用右上角搜索按鈕進行搜索文件 , file , 函數 , file數據linux , temp下文件被刪除 , The , Temporary , 臨時temp , file函數 , 臨時 unlink linux系統c語言編程、c語言編寫操作系統、c語言寫操作系統、c語言開發操作系統、c語言 操作系統,以便于您獲取更多的相關知識。
總結
以上是生活随笔為你收集整理的在linux系统下使用C语言操作临时文件的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java 多线程日志_多线程 打印的日志
- 下一篇: kali linux 清华源_Kali