10、【Linux系统编程】lseek函数

  每个打开的文件都记录着当前读写位置,打开文件时读写位置是0,表示文件开头,通常读写多少个字节就会将读写位置往后移多少个字节。但是有一个例外,如果以O_APPEND方式打开,每次写操作都会在文件末尾追加数据,然后将读写位置移到新的文件末尾。lseek和标准I/O库的fseek函数类似,可以移动当前读写位置(或者叫偏移量)。头文件及函数原型如下:

1 #include <sys/types.h>
2 #include <unistd.h>
3 off_t lseek(int fd, off_t offset, int whence);

  参数offset和whence的含义和fseek函数完全相同。只不过第一个参数换成了文件描述符。和fseek一样,偏移量允许超过文件末尾,这种情况下对该文件的下一次写操作将延长文件,中间空洞的部分读出来都是0。
  若lseek成功执行,则返回新的偏移量,因此可用以下方法确定一个打开文件的当前偏移量:

1 off_t currpos;
2 currpos = lseek(fd, 0, SEEK_CUR);

  这种方法也可用来确定文件或设备是否可以设置偏移量,常规文件都可以设置偏移量,而设备一般是不可以设置偏移量的。如果设备不支持lseek,则lseek返回-1,并将errno设置为ESPIPE。注意fseek和lseek在返回值上有细微的差别,fseek成功时返回0失败时返回-1,要返回当前偏移量需调用ftell而lseek成功时返回当前偏移量失败时返回-1。
一个小例子:(先要创建一个abc文件)

 1 #include <sys/types.h>
 2 #include <sys/stat.h>
 3 #include <fcntl.h>
 4 #include <errno.h>
 5 #include <stdio.h>
 6 #include <stdlib.h>
 7 #include <unistd.h>
 8 #include <iostream>
 9 using namespace std;
10 
11 int main()
12 {
13     int fd = open("abc",O_RDWR);
14     if(fd < 0)
15     {
16         cout << "open abc" << endl;
17         return 0;
18     }
19     //拓展文件大小,用od -tcx abc命令查看结果
20     lseek(fd, 0X1000,SEEK_SET);
21     write(fd, "a", 1);
22 
23     //用lseek获取文件大小
24     printf("abc's size:%d
",(int)lseek(fd,0,SEEK_END));
25 
26     close(fd);
27     return 0;
28 }
原文地址:https://www.cnblogs.com/Long-w/p/9437421.html