Linux伺服器編程之:truncate()函數+案例說明

來源:互聯網
上載者:User

標籤: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()函數+案例說明

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.