#include <sys/types.h>
#include <unistd.h>
off_t lseek(int fd, off_t offset, int whence);
lseek(int fd, off_t offset, int whence);
注:lseek(文件,偏移值,光标位置);
偏移值:为 0 不移动,大于 0 (正数)向后移动,小于 0 (负数)向前移动
光标位置:SEEK_SET 文件头
SEEK_CUR 文件当前位置
SEEK_END 文件尾部
lseek 函数返回值为:针对于文件头偏移的字节数。
可利用 lseek函数 计算文件的大小:
例:int size = lseek(fd,0,SEEK_END);
printf("file's size :%d\n",size);
int main()
{
int fd;fd=open("./file",O_RDWR);
int size = lseek(fd,0,SEEK_END);
printf("file's size :%d\n",size);
close(fd);
return 0;
}