Linux设置文件结束(缩小,截断,删除一些数据@ end)

ein*_*ude 4 linux system-calls

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

我如何在Linux中执行此操作?

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

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
Run Code Online (Sandbox Code Playgroud)

Kei*_*son 6

ftruncate(fd, 10);

(lseek不需要电话.)

man 2 ftruncate