Linux服务器编程之:truncate()函数+案例说明

来源:互联网 发布:数据字典是用来定义 编辑:程序博客网 时间:2024/05/22 05:06

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.案例说明:

 






0 0
原创粉丝点击