当前位置: 首页 > 编程笔记 >

Linux lseek函数的使用详解

马阳曦
2023-03-14
本文向大家介绍Linux lseek函数的使用详解,包括了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 (‘\0') 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 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中。

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

 类似资料:
  • 本文向大家介绍详解Swift中的函数及函数闭包使用,包括了详解Swift中的函数及函数闭包使用的使用技巧和注意事项,需要的朋友参考一下 一、引言 函数是有特定功能的代码段,函数会有一个特定的名称调用时来使用。Swift提供了十分灵活的方式来创建与调用函数。事实上在Swift,每个函数都是一种类型,这种类型由参数和返回值来决定。Swift和Objective-C的一大区别就在于Swift中的函数可以

  • 本文向大家介绍Python中的getopt函数使用详解,包括了Python中的getopt函数使用详解的使用技巧和注意事项,需要的朋友参考一下 函数原型: 参数解释:     args:args为需要解析的参数列表。一般使用sys.argv[1:],这样可以过滤掉第一个参数(ps:第一个参数是脚本的名称,它不应该作为参数进行解析)     shortopts:简写参数列表     longopts

  • 本文向大家介绍C语言putenv()函数和getenv()函数的使用详解,包括了C语言putenv()函数和getenv()函数的使用详解的使用技巧和注意事项,需要的朋友参考一下 C语言putenv()函数:改变或增加环境变量 头文件: 定义函数: 函数说明:putenv()用来改变或增加环境变量的内容. 参数string 的格式为name=value, 如果该环境变量原先存在, 则变量内容会依参

  • 本文向大家介绍Tensorflow:转置函数 transpose的使用详解,包括了Tensorflow:转置函数 transpose的使用详解的使用技巧和注意事项,需要的朋友参考一下 我就废话不多说,咱直接看代码吧! tf.transpose Defined in tensorflow/python/ops/array_ops.py. See the guides: Math > Matrix M

  • 本文向大家介绍python matplotlib中的subplot函数使用详解,包括了python matplotlib中的subplot函数使用详解的使用技巧和注意事项,需要的朋友参考一下 python里面的matplotlib.pylot是大家比较常用的,功能也还不错的一个包。基本框架比较简单,但是做一个功能完善且比较好看整洁的图,免不了要网上查找一些函数。于是,为了节省时间,可以一劳永逸。我

  • 本文向大家介绍C语言中fchdir()函数和rewinddir()函数的使用详解,包括了C语言中fchdir()函数和rewinddir()函数的使用详解的使用技巧和注意事项,需要的朋友参考一下 C语言fchdir()函数:改变当前工作目录 头文件: 定义函数: 函数说明:fchdir()用来将当前的工作目录改变成以参数fd 所指的文件描述词。 返回值:执行成功则返回 0, 失败返回-1, err