標籤:style blog http io os 使用 ar for 檔案
1.依賴標頭檔
#include<unistd.h>
#include<sys/types.h>
2.函數定義:
int truncate(const char *path,off_t length);
int ftruncate(int fd,off_t length);
函數說明:
The truncate() and ftruncate() functions cause the regular file named
by path or referenced by fd to be truncated to a size of precisely
length bytes.
翻譯:truncate()和ftruncate()函數導致一個名稱為path或者被檔案描述符fd引用的常規檔案被截斷成一個大小精為length位元組的檔案。
If the file previously was larger than this size, the extra data is
lost. If the file previously was shorter, it is extended, and the
extended part reads as null bytes (‘\0‘).
翻譯:如果先前的檔案大於這個大小,額外的資料丟失。如果先前的檔案小於當前定義的大小,那麼,這個檔案將會被擴充,擴充的部分將補以null,也就是‘\0’
If the size changed, then the st_ctime and st_mtime fields (respec‐
tively, time of last status change and time of last modification; see
stat(2)) for the file are updated, and the set-user-ID and set-group-ID
permission bits may be cleared.
翻譯:如果大小發生變化,那麼這個st_ctime(訪問時間)和st_mtime()修改時間將會被更新。
With ftruncate(), the file must be open for writing; with truncate(),
the file must be writable.
翻譯:使用ftruncate(),這個檔案必須被開啟用以寫操作。使用truncate函數的檔案必須能夠被寫
3.案例說明:
Linux伺服器編程之:truncate()函數+案例說明