lseek函数详解
- 首先看下函数:
off_t lseek(int fd, off_t offset, int whence);
- 所需要头文件:
#include <sys/types.h>
#include <unistd.h>
- 参数
fd 表示文件描述符
offset 表示相对于whence的位置
whence 可以是SEEK_SET(文件指针开始),SEEK_CUR(文件指针当前位置) ,SEEK_END为文件指针尾
- 返回值
文件读写指针距文件开头的字节大小,出错返回-1
用法
1) 欲将读写位置移到文件开头时:lseek(int fildes, 0, SEEK_SET);
2) 欲将读写位置移到文件尾时:lseek(int fildes, 0, SEEK_END);
3) 想要取得目前文件位置时:lseek(int fildes, 0, SEEK_CUR);