Linux set end of file (shrink, truncate, cut o

2019-05-04 18:20发布

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

How do I do this in 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

1条回答
成全新的幸福
2楼-- · 2019-05-04 18:42

ftruncate(fd, 10);

(The lseek call isn't needed.)

man 2 ftruncate

查看更多
登录 后发表回答