注:如果文章内容有误,请留言指出,谢谢合作 。
名字
【Linux lseek函数的使用详解】Name : lseek - reposition read/write file offset
lseek函数的作用是用来重新定位文件读写的位移 。
头文件以及函数声明
#include #include
描述
lseek() repositions the file offset of the open file description associated with the file descriptor fd to the argument offset according to the directive whence as follows:
SEEK_SET The file offset is set to offset bytes.
SEEK_CUR The file offset is set to its current location plus offset bytes.
SEEK_END The file offset is set to the size of the file plus offset bytes.
lseek() allows the file offset to be set beyond the end of the file (but this does not change the size of the file). If data is later written at this point, subsequent reads of the data in the gap (a “hole”) return null bytes (‘\0') until data is actually written into the gap.
lseek()函数会重新定位被打开文件的位移量,根据参数offset以及whence的组合来决定:
SEEK_SET:
从文件头部开始偏移offset个字节 。
SEEK_CUR:
从文件当前读写的指针位置开始,增加offset个字节的偏移量 。
SEEK_END:
文件偏移量设置为文件的大小加上偏移量字节 。
测试代码:
#include

文章插图
结果解析
观察offset以及dest_file和src_file文件的大小不难看出:程序通过lseek函数将src_file文件指针重新定位到文件末尾 + offset(注意,本程序对offset取了相反数,即文件末尾 + (-offset))处,然后从文件末尾 + offset处开始向前复制文件到dest_file中 。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持考高分网 。
- 春季老年人吃什么养肝?土豆、米饭换着吃
- 三八妇女节节日祝福分享 三八妇女节节日语录
- 老人谨慎!选好你的“第三只脚”
- 校方进行了深刻的反思 青岛一大学生坠亡校方整改校规
- 脸皮厚的人长寿!有这特征的老人最长寿
- 长寿秘诀:记住这10大妙招 100%增寿
- 春季老年人心血管病高发 3条保命要诀
- 眼睛花不花要看四十八 老年人怎样延缓老花眼
- 香槟然能防治老年痴呆症? 一天三杯它人到90不痴呆
- 老人手抖的原因 为什么老人手会抖
