linux的偏移函数,Linux C ftruncate函数清空文件注意事项_要使用lseek重置偏移量

66b52468c121889b900d4956032f1009.png

8种机械键盘轴体对比

本人程序员,要买一个写代码的键盘,请问红轴和茶轴怎么选?

ftruncate是改变文件长度的一个函数,一般在操作文件的时候用到,例如我之前写的log的时候,由于log是append only的,所以log的record会越老越多,但是某个时间点的时候,需要把文件中的log都读出来,并且汇总成一条log,也就是log的compat,这个时候,当把所有的log读出来的时候,就需要把以前的log全部删掉,并写入新compact的log。所以此时就会用到ftruncate。但是千万要注意的事情就是,用这个函数并不会真的把文件的offset置为0,需要用lseek重新seek一下。这篇文章介绍的很详细。当时主要还是函数说明没有看仔细。

之前有个要把打开的文件清空,然后重新写入的需求,但是使用 ftruncate(fd, 0)后,并没有达到效果,反而文件头部有了’ ’,长度比预想的大了。究其原因是没有使用 lseek 重置文件偏移量,是我太天真了,以为清空文件就会从头开始写入。

————————————- 我是解释分割线 ————————————–

首先 man ftruncate 看下帮助手册

NAME

truncate, ftruncate - truncate a file to a specified length

SYNOPSIS

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

int ftruncate(int fd, off_t length);

DESCRIPTION

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.

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 (‘ ’).

The file offset is not changed.

If the size changed, then the st_ctime and st_mtime fields (respectively, 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.

With ftruncate(), the file must be open for writing; with truncate(), the file must be writable.

之前就是因为没有看到红色那行字,导致我产生了文件开头的错误,都说了文件偏移量是不会改变的!

实验如下:1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28#include

#include

#include

#include

#include

#include

int main(void)

{

int fd;

const char *s1 = "0123456789";

const char *s2 = "abcde";

fd = open("test.txt", O_CREAT | O_WRONLY | O_TRUNC, 0666);

/* if error */

write(fd, s1, strlen(s1));

ftruncate(fd, 0);

// lseek(fd, 0, SEEK_SET);

write(fd, s2, strlen(s2));

close(fd);

return 0;

}

before.png

去掉lseek(fd, 0, SEEK_SET); 的注释后,效果如下:

after.png

结论:

从以上两张图中,可以看出,不用 lseek 的文件大小为15,用 xxd 查看16进制格式看到 文件头有10个 ‘ ’ 填充。

而重置文件偏移量后,文件大小为5,内容也正确。

因此,在用 ftruncate 函数时,再次写入一定要重新设置文件偏移量(在 ftruncate 之前或之后都行,用 lseek 或 rewind 都可以)。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值