记录 2 个 C 函数,这2个函数是用来截断一个文件的
int truncate(const char *path, off_t length);
int ftruncate(int fd, off_t length);
truncate:
/**
path : 文件路径
length : 截断的长度,设为 0 则可以清空文件。
若 length 大于文件大小,则文件被扩充,扩充部分用 '\0' 填充
**/
int truncate(const char *path, off_t length);
ftruncate:
/**
fd : 文件描述符,可以用 fileno() 函数获取已经打开文件的文件描述符
length : 截断的长度,设为 0 则可以清空文件。
若 length 大于文件大小,则文件被扩充,扩充部分用 '\0' 填充。
注意:截取后文件的偏移量并不会发生改变,需要我们重设文件偏移量。
在使用该函数前建议先使用 fflush() 刷新缓冲区,否则会出现截取后
文件尾部出现 (null) 的问题
**/
int ftruncate(int fd, off_t length);