关于 lseek 和 read

上次在使用 lseek 查找一个文件,其中在使用offset 的时候使用了负数,然后使用read去读取数据这时,read 不能读取出数据来,上述情况在ubuntu 8.04 && freebsd 4.x 下出现,但是在Red Hat Linux 2.6.x 下则没有这个问题,不知道是我的包含的header文件的问题,还是使用上有问题。

在使用lseek 去移动文件指针的时候都成功了,但是 使用read 读取数据的时候 则出现了上面的问题。

代码如下:

char info[12];
int fd = open("a.txt", O_RDONLY, 0666);
off_t pos = lseek(fd, -12, SEEK_END);
int ret = read(fd, &info, 12);

只好修改成了:
char info[12];
int fd = open("a.txt", O_RDONLY, 0666);
off_t pos = lseek(fd, 0, SEED_END);
int ret = pread(fd, &info, 12 , pos - 12);

才可以了。
This article is posted by on , link is .

Leave a reply