注:如果文章内容有误,请留言指出,谢谢合作。

名字

Name : lseek - reposition read/write file offset

lseek函数的作用是用来重新定位文件读写的位移。

头文件以及函数声明

#include

#include

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 (‘\0') until data is actually written into the gap.

lseek()函数会重新定位被打开文件的位移量,根据参数offset以及whence的组合来决定:

SEEK_SET:

从文件头部开始偏移offset个字节。

SEEK_CUR:

从文件当前读写的指针位置开始,增加offset个字节的偏移量。

SEEK_END:

文件偏移量设置为文件的大小加上偏移量字节。

测试代码:

#include

#include

#include

#include

#include

#include

#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 offset\n", 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: %d\n", 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中。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

linux lseek 指定 文件大小,Linux lseek函数的使用详解相关推荐

  1. linux lseek 指定 文件大小,linux下通过lseek()实现文件大小设置

    一 函数介绍: 函数名: lseek() 功 能: 移动文件读/写指针 所需头文件: #include #include 函数原型: off_t lseek(int fd, off_t offset, ...

  2. linux Shell(脚本)编程入门实例讲解详解

    linux Shell(脚本)编程入门实例讲解详解 为什么要进行shell编程 在Linux系统中,虽然有各种各样的图形化接口工具,但是sell仍然是一个非常灵活的工具.Shell不仅仅是命令的收集, ...

  3. 深入学习Linux摄像头(四)三星平台fimc驱动详解

    深入学习Linux摄像头系列 深入学习Linux摄像头(一)v4l2应用编程 深入学习Linux摄像头(二)v4l2驱动框架 深入学习Linux摄像头(三)虚拟摄像头驱动分析 深入学习Linux摄像头 ...

  4. Linux Shell脚本入门--wget 命令用法详解

    Linux Shell脚本入门--wget 命令用法详解 wget是在Linux下开发的开放源代码的软件,作者是Hrvoje Niksic,后来被移植到包括Windows在内的各个平台上.它有以下功能 ...

  5. 创建三个并发进程linux,Linux下几种并发服务器的实现模式(详解)

    1>单线程或者单进程 相当于短链接,当accept之后,就开始数据的接收和数据的发送,不接受新的连接,即一个server,一个client 不存在并发. 2>循环服务器和并发服务器 1.循 ...

  6. linux中cat、more、less命令区别详解

    linux中cat.more.less命令区别详解 转自:https://blog.csdn.net/xyw_blog/article/details/16861681 众所周知linux中命令cat ...

  7. 1 linux下tcp并发服务器的几种设计的模式套路,Linux下几种并发服务器的实现模式(详解)...

    1>单线程或者单进程 相当于短链接,当accept之后,就开始数据的接收和数据的发送,不接受新的连接,即一个server,一个client 不存在并发. 2>循环服务器和并发服务器 1.循 ...

  8. linux系统四个组成部分,Linux系统由哪几部分组成?系统详解(干货)

    原标题:Linux系统由哪几部分组成?系统详解(干货) 我们常说的Linux一般指的是系统内核,基于Linux系统内核的操作系统叫Linux发行版操作系统,像redhat.centos.ubuntu和 ...

  9. linux在vi创建文件,Linux下创建文本文件(vi/vim命令使用详解)

    vi test.txt 或者 vim test.txt 再或者 touch test.txt vim是vi的升级版,指令更多,功能更强. 下面是收集的vim用法,当在vim里面要实现退出,首先要做的是 ...

最新文章

  1. 网传阿里一总裁 PPT 被员工拍照泄漏,新规划遭曝光
  2. RAC+单实例DATAGUARD 配置
  3. 函数sigqueue
  4. 算法题---最长公共前缀
  5. Unity3d高频率面试题目(选择题)
  6. python echo off_生活中的python-随机分配单词输出至word
  7. delphi与java_Delphi XE8中Delphi和JAVA数据类型对应关系!
  8. python列表购物
  9. 用vs2008创建运行c++项目
  10. 基于Faster R-CNN的安全帽目标检测
  11. Identity of indiscernibles(不可分与同一性)
  12. PHP常用代码大全(新手入门必备)
  13. 公众号封面图内容数据提取软件
  14. 三天打鱼两天晒网python程序_三天打鱼两天晒网
  15. java实现加减乘除_用Java编写实现加减乘除,界面如下
  16. Keras-yolov3计算验证集acc一直为0
  17. DevEco Studio中文、鸿蒙IDE汉化
  18. 南京理工大学计算机学号6,学生学籍管理系统
  19. python 圆形的词云
  20. php 开启常驻进程,swoole如何常驻进程

热门文章

  1. no SQ lines present in the header解决方案
  2. 【服务器托管、租用】
  3. IDEA中配置Tomcat(详细教程)
  4. 计算机函数公式left,Excel中如何使用Left函数?
  5. Tomcat 部署 war 包
  6. 3天怒肝5万字!阿里P7大佬手写MySQL超全笔记,还担心学不会吗?
  7. 如何让 keil MDK v5 支持 ARM7/9 设备
  8. 锂电池办理IEC62133认证、CB认证、EN62133认证、UN38.3检测报告
  9. 【连载】【FPGA黑金开发板】NIOS II那些事儿--NIOS II 常见问题(FAQ)
  10. Gir合并merge两个完全不同Git项目时出现fatal: refusing to merge unrelated histories的解决办法