如果生成的文件偏移量对于常规文件是负的,则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=-1和errno设置EINVAL.
这也会导致文件大小显得非常大(接近无符号整数的大小) - 看起来像是一个溢出问题.这是为什么?