文件偏移

Linux中可以使用系统函数lseek来修改文件偏移量(读写位置)

每个打开的文件都记录着当前读写位置,打开文件时读写位置是0,表示文件开头,通常读写多少个字节就会将读写位置往后移多少个字节。但是有一个例外,如果以O_APPEND方式打开,每次写操作都会在文件末尾追加数据,然后将读写位置移到新的文件末尾。lseek和标准I/O库的fseek函数类似,可以移动当前读写位置(或者叫偏移量)。

回忆fseek的作用及常用参数。SEEK_SET、SEEK_CUR、SEEK_END

int fleek(FILE *stream,long offset,int whence);

成功返回0;失败返回-1

特别的:超出文件末尾位置返回0;往回超出文件头位置,返回-1

off_t lseek(int fd,off_t offset,int whence);

参数:

        fd:文件描述符

        offset:偏移量

        whence:起始偏移位置,SEEK_SET/SEEK_CUR/SEEK_END

返回值:

        成功:较起始位置偏移量(获取文件大小,)

        失败:-1 errno

特别的:lseek允许超过文件结尾设置偏移量,文件会因此被扩展。

注意文件“读”和“写”使用同一偏移位置

失败:返回-1;

成功:返回的值是较文件起始位置向后的偏移量。

特别的:lseek允许超过文件末尾设置偏移量,文件会因此被拓展。

     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函数是文件起始向后的偏移长度

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <fcntl.h>
int main(void)
{int fd,n;char msg[]="It's a test for lseek\n"char ch;fd=open("lseek.txt",O_RDWR|O_CREAT,0664);if(fd<0){perror("open lseek.txt error");exit(1);}write(fd,msg,strlen(msg)); //使用fd对打开的文件进行写操作,文件读写位置位于文件结尾处while((n = read(fd,&ch,1)))//读取数据时,条件为真一直读取,读到文件末尾为止{if(n<0){perror("read error");exit(1);}write(STDOUT_FILENO,&ch,n);  //将文件内容按字节读出,写出到屏幕}close(fd);return 0;
}

lseek(fd,0,SEEK_SET):从起始位置开始偏移0个字节

把它显示到屏幕上

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <fcntl.h>
int main(void)
{int fd,n;char msg[]="It's a test for lseek\n";char ch;fd=open("lseek.txt",O_RDWR|O_CREAT,0777);if(fd<0){perror("open lseek.txt error");exit(1);}write(fd,msg,strlen(msg));lseek(fd,0,SEEK_SET); //修改文件读写指针位置,位于文件开头。while((n = read(fd,&ch,1))){if(n<0){perror("read error");exit(1);}write(STDOUT_FILENO,&ch,n);}close(fd);return 0;
}                                                                                                                                                                                                           

文件读和写使用同一偏移位置

应用场景:

1.文件的“读”、“写” 使用同一偏移位置。

2.使用lseek获取、扩展文件大小。

打开一个文件,将偏移调到文件末尾,返回值就是文件大小

3.使用lseek拓展文件大小:想要文件大小真正拓展,必须引起IO操作

用lseek函数获取文件大小

#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
int main(int argc,char *argv[])
{int fd = open(argv[1],O_RDWR);if(fd == -1){perror("open error");exit(1);}int len = lseek(fd,0,SEEK_END);printf("file size :%d\n",len);return 0;
}

执行结果如下:

扩展文件大小:

#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
int main(int argc,char *argv[])
{int fd = open(argv[1],O_RDWR);if(fd == -1){perror("open error");exit(1);}int len = lseek(fd,11,SEEK_END);printf("file size :%d\n",len);return 0;
}

想要文件大小真正拓展,以下可以使用以下方法:

#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
int main(int argc,char *argv[])
{int fd = open(argv[1],O_RDWR);if(fd == -1){perror("open error");exit(1);}int len = lseek(fd,110,SEEK_END);printf("file size :%d\n",len);write(fd,"a",1);close(1);return 0;
}

lseek常用应用:

1.使用lseek拓展文件:write操作才能实质性的拓展文件。单lseek是不能进行拓展的。

一般:write(fd,"a",1);


od-tcx filename    查看文件的16进制表示形式

od-tcd filename    查看文件的10进制表示形式

2.通过lseek获取文件的大小:lseek(fd,0,SEEK_END);

【最后注意】:lseek函数返回的偏移量总是相对于文件头而言。

truncate函数也可以拓展文件大小

成功:返回值是0

失败:返回值是-1

使用truncate函数直接拓展文件

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
int main(int argc,char **argv)
{int ret=truncate("dict.cp",250);printf("ret = %i\n",ret);return 0;
}

27.Linux lseek函数相关推荐

  1. linux lseek 指定 文件大小,Linux lseek函数的使用详解

    注:如果文章内容有误,请留言指出,谢谢合作. 名字 Name : lseek - reposition read/write file offset lseek函数的作用是用来重新定位文件读写的位移. ...

  2. Linux lseek函数

    接口:off_t lseek(int fd, off_t offset, int whence); 头文件: #include<sys/types.h> #include<unist ...

  3. 【Linux系统IO函数】lseek函数

    Linux系统IO函数-lseek函数 1.1 lseek函数与标准C库的fseek函数 lseek函数对应标准C库中的fseek函数 查看标准C库中的fseek函数使用说明: (shell输入) m ...

  4. linux文件编程(open、write、read、creat、lseek函数)

    文件编程内容比较多,如文件系统原理及访问机制文件在内核中的管理机制,什么是文件信息节点iNode.文件共享.文件权限.各种用户对其权限等等.以下主要记录如何用代码操作文件,实现文件的创建.打开.编辑等 ...

  5. Linux系统调用之lseek函数

    前言 如果,想要深入的学习Linux系统调用函数lseek了话,还是需要去阅读Linux系统中的帮助文档的. 具体输入命令: man 2 lseek 即可查阅到完整的资料信息. lseek函数 lse ...

  6. Linux 系统 IO之 lseek 函数

    Linux 系统 IO之 lseek 函数 文章目录 Linux 系统 IO之 lseek 函数 1. lseek 函数 1.1 头文件包含 1.2 函数原型 1.3 函数功能 1.4 函数返回值 1 ...

  7. linux用Lseek函数 拷贝文件,linux系统下lseek函数的详细用法

    前面的文章里面,仔细讲了在linux系统对文件的读写操作以及文件管理,为今天要讲的内容作了铺垫(如果您是刚接触这方面的内容,可以先看我之前写的文章,有错误的地方,还望指出来,在这里先说一声谢谢).好了 ...

  8. Linux编程基础之lseek函数返回值

    目录 前言 lseek函数返回值 实操证明 书的原图 总结 前言 操作系统:Fedora 头文件 <sys/types.h> , <unistd.h> lseek函数返回值 o ...

  9. Linux文件I/O编程(二)lseek函数

    文件I/O编程处理open.read.write.close,等必要函数对文件进行读写操作外,lseek.fcntl也是I/O编程很重要的函数. lseek函数 lseek函数主要用来移动当前读写位置 ...

最新文章

  1. C++数据结构链表的基本操作
  2. c语言断链隐藏dll,通过断链隐藏模块(DLL)
  3. mysql distinct多个字段_深入浅出Mysql索引的那些事儿
  4. phpstudy升级mysql之后,修改配置文件无效的问题
  5. Walking on the path of Redis --- Redis configuration
  6. 为什么要划分物料组_SAP
  7. 使用之location和rewrite用法
  8. Oracle报错:类型长度大于最大值解决办法
  9. activity多实例任务减签
  10. 爬虫新宠requests_html 带你甄别2019虚假大学 #华为云·寻找黑马程序员#
  11. Python中threading的join和setDaemon的区别及用法[例子]
  12. python编写命令行框架_使用 Python 和 Click 编写命令行应用程序
  13. 2017年最具价值的十大开源项目:tensorflow 第一
  14. sql注入工具的使用
  15. UTF-8与GBK互转,为什么会乱码
  16. 方向导数与梯度——学习笔记
  17. zoj3869 Ace of Aces zoj3880 Demacia of the Ancients(水)
  18. 文件读取漏洞路径收集
  19. 黑轴、青轴、茶轴、红轴、白轴的区别
  20. Dynamic Head Unifying Object Detection Heads with Attentions 论文阅读笔记

热门文章

  1. java计算机毕业设计教学质量测评系统源码+系统+数据库+lw文档
  2. 暑假N天乐 —— 01背包及变形
  3. Asp.Net MVC导出下载excel
  4. oracle判断字段是否包含某个字符串
  5. Python小游戏——外星人入侵(保姆级教程)第一章 07调整飞船速度 08限制飞船活动范围
  6. 【Office安装问题】
  7. PTA: 小H小W爱魔法
  8. Python 使用循环结构输出菱形(倒等边三角形)图案
  9. Python实现性能测试(locust)
  10. Win10 U盘安装步骤