2008-12-10 35 views

回答

5

您可以使用truncate function

int truncate(const char *path, off_t length);

從手冊頁:

「的截斷()和ftruncate()函數引起的路徑指定的或由FD引用的常規文件被截斷的大小的精確長度字節。如果文件以前是超過此大小的,額外的數據丟失。如果文件以前是短,它擴展,並且擴展部分內容空字節」

+0

謝謝,但這是便攜式? – codymanix 2008-12-10 23:38:53

3
#include <unistd.h> 
    #include <sys/types.h> 

    int truncate(const char *path, off_t length); 
    int ftruncate(int fd, off_t length); 

從它的手冊頁:

的截斷()和ftruncate() 功能導致命名 由路徑或通過FD引用是 截斷爲一個尺寸精確 長度字節的常規文件。

如果以前的文件比這個大小大 ,額外的數據是 丟失。如果文件先前爲 更短,則將其擴展,並且 擴展部分讀取爲空字節 ('\ 0')。