小编Tod*_*wer的帖子

为什么lseek允许我设置负文件位置?

如果生成的文件偏移量对于常规文件是负的,则lseek应该返回man条目-1.

为什么这样的代码有效?

int fd;
off_t offset;

fd = open("test.txt", O_RDWR);
offset = lseek(fd, -10, SEEK_SET);

printf("%d\n", offset);                // prints -10

if (offset == (off_t) -1)
    perror("seek");                    // error not triggered
Run Code Online (Sandbox Code Playgroud)

我觉得我应该得到offset=-1errno设置EINVAL.

这也会导致文件大小显得非常大(接近无符号整数的大小) - 看起来像是一个溢出问题.这是为什么?

c linux

2
推荐指数
2
解决办法
3502
查看次数

标签 统计

c ×1

linux ×1