歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> 關於Linux >> 在linux系統下使用C語言操作臨時文件

在linux系統下使用C語言操作臨時文件

日期:2017/3/3 16:40:28   编辑:關於Linux

有時程序需要存儲很大量的數據,或者在幾個進程間交換數據,這時您可能考慮到使用臨時文件。使用臨時文件要考慮幾個問題:

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. 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等函數,但是由於健壯性和安全方面理由不建議使用他們。

Copyright © Linux教程網 All Rights Reserved