Linux set end of file (shrink, truncate, cut out some data @ end)

强颜欢笑 提交于 2019-12-22 09:01:25

问题


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:


ftruncate(fd, 10);

(The lseek call isn't needed.)

man 2 ftruncate



来源:https://stackoverflow.com/questions/7328489/linux-set-end-of-file-shrink-truncate-cut-out-some-data-end

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!