linux上lseek的理解
来源:互联网 发布:内控优化年心得体会 编辑:程序博客网 时间:2024/06/05 19:31
每个打开文件都有一个与其相关联的"当前文件位移量"。它是一个非负整数,用以度量从文件开始处计算的字节数。系统默认当打开一个文件
时,除非指定O_APPEND选择项,否则该位移量被设置为0。即始终指向下一个要读或写的位置。
lseek仅将当前的文件位移量记录在内核内,它并不引起任何I/O操作。然后,该位移量用于下一个读或写操作。
文件位移量可以大于文件的当前长度,在这种情况下,对该文件的下一次写将延长该文件,并在文件中构成一个空洞,
这一点是允许的。位于文件中但没有写过的字节都读写为0.
下面创建一个含有空洞的文件
#include<fcntl.h>#include<unistd.h>#include<stdio.h>int main(){int id = open("file.hole",O_WRONLY|O_CREAT,S_IRUSR|S_IWUSR);if(id < 0){perror("open error");return 0;}int n = write(id,"abcdefghij",10);if(n != 10){perror("write error");return 0;}if(lseek(id,40,SEEK_SET) == -1){perror("lseek error");return 0;}n = write(id,"abcdefghij",10);if(n != 10){perror("write error");return 0;}close(id);return 0;}
hexdump -c file.hole
0000000 a b c d e f g h i j \0 \0 \0 \0 \0 \0
0000010 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0
0000020 \0 \0 \0 \0 \0 \0 \0 \0 a b c d e f g h
0000030 i j
然后再打开空洞文件,先lseek定位到40,再读再写。
#include<fcntl.h>#include<unistd.h>#include<stdio.h>int main(){int id = open("file.hole",O_RDWR);if(id < 0){perror("open error");return 0;}if(lseek(id,40,SEEK_SET) == -1){perror("lseek error");return 0;}char buf[100]={0};int n = read(id,buf,5);printf("%s\n",buf);n = write(id,"pstxyz",3);if(n != 3){perror("write error");return 0;}close(id);return 0;}
./lseek2.exe
abcde
<mct>hexdump -c file.hole
0000000 a b c d e f g h i j \0 \0 \0 \0 \0 \0
0000010 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0
0000020 \0 \0 \0 \0 \0 \0 \0 \0 a b c d e p s t
0000030 i j
0000032
可以看出,在写的时候是覆盖,而不是插入。
- linux上lseek的理解
- linux下lseek()函数理解
- linux lseek
- Linux ---lseek() 函数
- linux文件函数-lseek
- Linux ---lseek() 函数
- linux open lseek
- Linux ---lseek() 函数
- Linux---lseek函数
- linux下的文件的拓展,由lseek实现
- linux下的open()read()write()close()lseek()函数
- Linux 通过lseek()来实现文件大小的设置
- Linux 通过lseek()来实现文件大小的设置
- Linux下 fseek() ftell() rewind() lseek() fstat() 的使用方法
- Linux文件I/O的lseek,fcntl和ioctl函数
- Linux 通过lseek()来实现文件大小的设置
- Linux C语言中的read write lseek的使用
- Linux 通过lseek()来实现文件大小的设置
- JavaScript权威指南-读书笔记2
- 杭电1051 Wooden Sticks (贪心)
- tcp/ip 归纳
- hadoop2.2+zk ha环境搭建
- Gionee/金立C500 root教程_方法
- linux上lseek的理解
- cannot find the tag library descriptor for http://java.sun.com/jsp/jstl/core
- [剑指offer]面试题40:数组中只出现一次的数字
- ScrollView 内置百度地图 地图监听被截获问题
- mvn
- Coolpad/酷派7231 root教程_方法
- NYOJ 45 棋盘覆盖
- hdu 4911 Inversion (逆序数变换)
- 同步和异步的区别