Linux lseek函数的使用详解
注:如果文章内容有误,请留言指出,谢谢合作。
名字
Name : lseek – reposition read/write file offset
lseek函数的作用是用来重新定位文件读写的位移。
头文件以及函数声明
#include <sys/types.h>
#include <unistd.h>
off_t lseek(int fd, off_t offset, int whence);
offset为正则向文件末尾移动(向前移),为负数则向文件头部(向后移)。
描述
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 (‘ ′) until data is actually written into the gap.
lseek()函数会重新定位被打开文件的位移量,根据参数offset以及whence的组合来决定:
SEEK_SET: 从文件头部开始偏移offset个字节。 SEEK_CUR: 从文件当前读写的指针位置开始,增加offset个字节的偏移量。 SEEK_END: 文件偏移量设置为文件的大小加上偏移量字节。
测试代码:
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
#include <stdio.h>
#define BUFFER_SIZE 1024
#define SRC_FILE_NAME "src_file"
#define DEST_FILE_NAME "dest_file"
//根据传入的参数来设置offset
#define OFFSET (atoi(args[1]))
int main(int argc, char*args[]) {
int src_file, dest_file;
unsigned char buff[BUFFER_SIZE];
int real_read_len, off_set;
if (argc != 2) {
fprintf(stderr, "Usage: %s offsetn", args[0]);
exit(-1);
}
src_file = open(SRC_FILE_NAME, O_RDONLY);
dest_file = open(DEST_FILE_NAME, O_WRONLY | O_CREAT, S_IREAD | S_IWRITE );//owner权限:rw
if (src_file < 0 || dest_file < 0) {
fprintf(stderr, "Open file error!n");
exit(1);
}
off_set = lseek(src_file, -OFFSET, SEEK_END);//注意,这里对offset取了相反数
printf("lseek() reposisiton the file offset of src_file: %dn", off_set);
while((real_read_len = read(src_file, buff, sizeof(buff))) > 0) {
write(dest_file, buff, real_read_len);
}
close(dest_file);
close(src_file);
return 0;
}
结果解析
观察offset以及dest_file和src_file文件的大小不难看出:程序通过lseek函数将src_file文件指针重新定位到文件末尾 + offset(注意,本程序对offset取了相反数,即文件末尾 + (-offset))处,然后从文件末尾 + offset处开始向前复制文件到dest_file中。
以上就是本文的全部内容,希望对大家的学习有所帮助。
- 教你如何去掉友荐和无觅的隐藏外链和版权链接
- 重置多说配置后的问题,这是不让我从良的节奏啊(附禁用谷歌在线字体的方法)!
- 10个超有趣的Linux命令
- 张戈博客惊现WordPress恶意代码,各位WP博主要注意下了!
- 分享一个多说头像的动态酷炫CSS样式
- 在Linux中发现IP地址冲突的方法
- 教你如何查看Linux的CPU负载
- 想打造一个神经网络,自动给黑白照片上色?这儿有一份超详细教程
- WP_Widget_PostViews has a deprecated constructor 报错解决
- 详解Linux系统的CPU负载均值
- Linux服务器的进程查看命令详解
- 另类SEO分享:利用JS封装iframe躲过搜索引擎的抓取
- Linux :MAC 地址克隆方法
- 解决WordPress修改数据库表前缀后无法登陆的问题
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- 基于NXP i.MX28 MPU的疫情监控平台3-监控平台实现
- 基于NXP i.MX28 MPU的疫情监控平台2-Qt环境搭建
- 基于NXP i.MX28 MPU的疫情监控平台1-交叉编译环境搭建
- API接口设计规范
- 陶瓷PCB电路板到底好在哪?
- Mysql常用函数——告别复杂sql
- Spring Boot读取配置文件的几种方式
- python菜鸟教程 | if else 判断
- Java 语言中十大“坑爹”功能!
- R语言生存分析可视化分析
- 全平台轻量开源verilog仿真工具iverilog+GTKWave使用教程
- R语言时间序列和ARIMA模型预测拖拉机销售的制造案例研究
- Windows10下安装linux子系统学习笔记
- 项目实践 | 从零开始学习Deep SORT+YOLO V3进行多目标跟踪(附注释项目代码)
- [OHIF-Viewers]医疗数字阅片-医学影像-Module: Panel-自定义面板-中-es6-Object.defineProperty()定义属性