国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學(xué)院 > 開發(fā)設(shè)計 > 正文

在linux下使用c語言操作臨時文件

2019-11-17 05:03:01
字體:
供稿:網(wǎng)友
/******************************************************************
*本文首發(fā)于bbs.bluegem.org的CWorld區(qū)
*本人email:chenfei@sohu.com
*如轉(zhuǎn)載本文,請保留首發(fā)地和本人聯(lián)絡(luò)方式,以方便交流,
謝謝!
******************************************************************/ 
    有時程序需要存儲很大量的數(shù)據(jù),或者在幾個進(jìn)程間交換數(shù)據(jù),這時您可能考慮到使用臨時文件。使用臨時文件要考慮幾個問題:
1、保證臨時文件間的文件名不互助沖突。
2、保證臨時文件中內(nèi)容不被其他用戶或者黑客偷看、刪除和修改。
所以在linux下有專門處理臨時文件的函數(shù)
mkstemp函數(shù)
    mkstemp函數(shù)將在系統(tǒng)中以獨一無二的文件名創(chuàng)建一個文件并打開,而且只有當(dāng)前用戶才有訪問這個臨時文件的權(quán)限,當(dāng)前用戶對這個臨時文件可以打開并進(jìn)行讀、寫操作。mkstemp函數(shù)只有一個參數(shù),這個參數(shù)是個以“XXXXXX”結(jié)尾的非空字符串。mkstemp函數(shù)會用隨機產(chǎn)生的字符串替換“XXXXXX”,保證了文件名的唯一性。函數(shù)返回一個文件描述符,假如執(zhí)行失敗返回-1。在glibc 2.0.6 以及更早的glibc庫中這個文件的訪問權(quán)限是0666 ,glibc 2.0.7 以后的庫這個文件的訪問權(quán)限是0600。
    當(dāng)臨時文件完成她的使命假如不把它清除干凈把或者程序由于意外在臨時文件被清除前就已經(jīng)退出,臨時文件所在的目錄會塞滿垃圾。由于mkstemp函數(shù)創(chuàng)建的臨時文件不能自動刪除(請參考下文中的tmpfile函數(shù))。執(zhí)行完mkstemp函數(shù)后要調(diào)用unlink函數(shù),unlink函數(shù)刪除文件的目錄入口,所以臨時文件還可以通過文件描述符進(jìn)行訪問,直到最后一個打開的進(jìn)程關(guān)閉文件操作符,或者程序退出后臨時文件被自動徹底地刪除。
例程:
直接使用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. */
/*在這函數(shù)從BUFFER中向臨時文件寫入LENGTH字節(jié)數(shù)據(jù)。臨時文件在剛一創(chuàng)建就被刪除掉。函數(shù)會返回臨時文件的句柄。*/
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將被隨機字符串代替,以保證文件名在系統(tǒng)中的唯一性*/
 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,這樣只要文件描述符一關(guān)閉文件就會被自動刪除*/
 unlink (temp_filename);
 /* Write the number of bytes to the file first. */
 /*首先寫入即將寫入數(shù)據(jù)的長度*/
 write (fd, &length, sizeof (length));
 /* Now write the data itself. */
 /*寫入數(shù)據(jù)本身*/
 write (fd, buffer, length);
 /* Use the file descriptor as the handle for the temporary file. */
 /*函數(shù)返回文件描述符,作為臨時文件的句柄*/
 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創(chuàng)建的臨時文件中讀取數(shù)據(jù)。返回值是含有文件內(nèi)容的新申請到的內(nèi)存塊,這塊內(nèi)存應(yīng)該又調(diào)用read_temp_file者釋放。
*length是臨時文件正文內(nèi)容的長度。執(zhí)行完read_temp_file函數(shù)后臨時文件被徹底刪除*/
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. */
 /*分配內(nèi)存塊,讀取數(shù)據(jù)*/
 buffer = (char*) malloc (*length);
 read (fd, buffer, *length);
 /* Close the file descriptor, which will cause the temporary file to
 go away. */
 /*關(guān)閉文件描述符,臨時文件被徹底刪除*/
 close (fd);
 return buffer;
}


tmpfile函數(shù)
    假如您使用C library I/O函數(shù),并且并沒有另一個程序使用這個臨時文件(筆者注:按我的理解是在同一進(jìn)程或具有父子關(guān)系的進(jìn)程組中),有個更簡潔的函數(shù)——tmpfile。tmpfile函數(shù)創(chuàng)建并打開一個臨時文件,并且自動執(zhí)行了unlink了這個臨時文件。tmpfile函數(shù)返回一個文件描述符,假如執(zhí)行失敗返回NULL。當(dāng)程序執(zhí)行了fclose或者退出時,資源被釋放。
    linux系統(tǒng)中還提供mktemp、 tmpnam、 和tempnam等函數(shù),但是由于健壯性和安全方面理由不建議使用他們。
更多文章 更多內(nèi)容請看C/C++進(jìn)階技術(shù)文檔  C++編程  Linux文件相關(guān)文章專題,或

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 普兰县| 瑞安市| 泉州市| 望江县| 罗定市| 类乌齐县| 上林县| 巴青县| 喜德县| 措勤县| 九寨沟县| 邳州市| 樟树市| 泗水县| 合川市| 陆良县| 政和县| 东明县| 宁乡县| 盘山县| 翁源县| 买车| 喀喇| 东平县| 广南县| 泰和县| 盐城市| 遂昌县| 措勤县| 洛浦县| 女性| 南投县| 利辛县| 兴海县| 青州市| 子洲县| 滦平县| 镇江市| 枣阳市| 金坛市| 高青县|