Linux设置文件结尾(缩小,截断,在结尾处切出一些数据) [英] Linux set end of file (shrink, truncate, cut out some data @ end)

查看:55
本文介绍了Linux设置文件结尾(缩小,截断,在结尾处切出一些数据)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Windows中,有SetEndOfFile()API可以最终切出一些数据.

In windows there is SetEndOfFile() API to cut out some data in the end.

如何在Linux中做到这一点?

How do I do this in Linux?

我正在寻找的伪代码示例(特定于 Linux):

The pseudo-code sample of what I'm looking for (Linux-specific):

int fd = open("/path/to/file",O_RDWR);
// file contents: "0123456789ABCDEF", 16 bytes
lseek(fd,10,SEEK_CUR);
// what's in the next line? (imaginary code)
syscall(what,fd,FD_SET_EOF);

close(fd);
//sync();
// now file on disk looks like "0123456789", 10 bytes

推荐答案

ftruncate(fd,10);

(不需要 lseek 调用.)

man 2 ftruncate

这篇关于Linux设置文件结尾(缩小,截断,在结尾处切出一些数据)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆