lseek函数的用法
所有打開的文件都有一個當前文件偏移量(current file offset),以下簡稱為 cfo。cfo 通常是一個非負整數,用于表明文件開始處到文件當前位置的字節數。讀寫操作通常開始于 cfo,并且使 cfo 增大,增量為讀寫的字節數。文件被打開時,cfo 會被初始化為 0,除非使用了O_APPEND。
使用 lseek 函數可以改變文件的 cfo 。
#include<unistd.h>
#include <sys/types.h>
off_t lseek(int filedes, off_t offset, int whence);
返回值:新的偏移量(成功),-1(失?。?/p>
參數 offset 的含義取決于參數 whence:
1. 如果 whence 是SEEK_SET,文件偏移量將被設置為 offset。
2. 如果 whence 是SEEK_CUR,文件偏移量將被設置為 cfo 加上 offset,
offset 可以為正也可以為負。
3. 如果 whence 是SEEK_END,文件偏移量將被設置為文件長度加上 offset,
offset 可以為正也可以為負。
SEEK_SET、SEEK_CUR 和 SEEK_END 是 System V 引入的,在這之前使用的是 0、1 和 2。
lseek 的以下用法返回當前的偏移量:
off_t currpos;
currpos = lseek(fd, 0, SEEK_CUR);
這個技巧也可用于判斷我們是否可以改變某個文件的偏移量。如果參數 fd(文件描述符)指定的是 pipe(管道)、FIFO 或者 socket,lseek 返回 -1 并且置 errno 為ESPIPE。
對于普通文件(regular file),cfo 是一個非負整數。但對于特殊設備,cfo有可能是負數。因此,我們不能簡單地測試 lseek 的返回值是否小于 0 來判斷 lseek 成功與否,而應該測試 lseek 的返回值是否等于 -1 來判斷 lseek 成功與否。
lseek 僅將 cfo 保存于內核中,不會導致任何 I/O 操作。這個 cfo 將被用于之后的讀寫操作。
如果 offset 比文件的當前長度更大,下一個寫操作就會把文件“撐大(extend)”。這就是所謂的在文件里創造“空洞(hole)”。沒有被實際寫入文件的所有字節由重復的 0表示??斩词欠裾加糜脖P空間是由文件系統(file system)決定的。
以下程序創建一個有空洞的文件:
/* Standard C header */
#include<stdio.h>
/* Unix header */
#include<fcntl.h>
#include<unistd.h>
#include<sys/stat.h>
char buf1[] = “abcdefghij”;
char buf2[] = “ABCDEFGHIJ”;
int main(void)
{
int fd, size;
if ((fd = creat(“file.hole”, S_IRUSR|S_IWUSR)) < 0)
{
printf(“creat error\n”);
return -1;
}
size = sizeof buf1 – 1;
if (write(fd, buf1, size) != size)
{
printf(“buf1 write error\n”);
return -1;
}
/* offset now = 10 */
if (lseek(fd, 16384, SEEK_SET) == -1)
{
printf(“lseek error\n”);
return -1;
}
/* offset now = 16384 */
size = sizeof buf2 – 1;
if (write(fd, buf2, size) != size)
{
printf(“buf2 write error\n”);
return -1;
}
/* offset now = 16394 */
return 0;
}
摘自:http://blog.csdn.net/songyang516/article/details/6779950
同時也可以參考:http://baike.baidu.com/view/1080860.htm
總結
以上是生活随笔為你收集整理的lseek函数的用法的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: SAP Spartacus State
- 下一篇: SAP Spartacus RouteR