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

简介: 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()

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

 






目录
相关文章
|
5天前
|
Linux
【Linux系统编程】基础指令(二)(下)
【Linux系统编程】基础指令(二)
|
5天前
|
Linux C语言
【Linux系统编程】基础指令(二)(上)
【Linux系统编程】基础指令(二)
|
3天前
|
Linux Shell 调度
【Linux系列】fork( )函数原理与应用详解——了解【父子进程及其特性】(代码演示,画图帮助理解,思维导图,精简)(11)
【Linux系列】fork( )函数原理与应用详解——了解【父子进程及其特性】(代码演示,画图帮助理解,思维导图,精简)(11)
|
4天前
|
Prometheus 监控 Cloud Native
构建高效可靠的Linux服务器监控体系
【4月更文挑战第30天】 在维护企业级Linux服务器的稳定性和性能方面,一个周全的监控体系是至关重要的。本文将探讨如何利用开源工具和实践构建一个高效、灵活且用户友好的监控系统。我们将重点讨论核心组件的选择、配置、报警机制以及数据分析方法,旨在帮助读者打造一个能够实时响应并预防潜在问题的监控环境。
|
5天前
|
存储 Unix Linux
【Linux系统编程】基础指令(三)
【Linux系统编程】基础指令(三)
|
5天前
|
Linux
【Linux系统编程】基础指令(一)(下)
【Linux系统编程】基础指令(一)
|
5天前
|
人工智能 Unix Linux
【Linux系统编程】基础指令(一)(上)
【Linux系统编程】基础指令(一)
|
5天前
|
Unix 大数据 Linux
【Linux系统编程】Linux背景知识
【Linux系统编程】Linux背景知识
|
6天前
|
安全 Linux Shell
Linux:探索开源之魅与编程之道
Linux:探索开源之魅与编程之道
17 4
|
6天前
|
缓存 网络协议 Linux
Linux服务器性能优化小结(下)
Linux服务器性能优化小结(下)
16 0