Detailed explanation of the use of Linux lseek function

Detailed explanation of the use of Linux lseek function

Note: If there are any errors in the article, please leave a message to point them out. Thank you for your cooperation.

name

Name : lseek - reposition read/write file offset

The lseek function is used to reposition the file reading and writing displacement.

Header files and function declarations

#include <sys/types.h>
#include <unistd.h>
off_t lseek(int fd, off_t offset, int whence);

If the offset is positive, it moves toward the end of the file (moving forward), and if it is negative, it moves toward the beginning of the file (moving backward).

describe

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.

The lseek() function repositions the displacement of the opened file, which is determined by the combination of the offset and whence parameters:

SEEK_SET:
The offset is offset bytes from the beginning of the file.
SEEK_CUR:
Starting from the current read/write pointer position of the file, increase the offset by offset bytes.
SEEK_END:
The file offset is set to the size of the file plus offset bytes.

Test code:

#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"
//Set the offset according to the passed parameters
#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 permission: rw
  if (src_file < 0 || dest_file < 0) {
    fprintf(stderr, "Open file error!\n");
    exit(1);
  }
  off_set = lseek(src_file, -OFFSET, SEEK_END); //Note that the offset is negated here 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;
}

Result interpretation

By observing the offset and the sizes of the dest_file and src_file files, it is not difficult to see that the program uses the lseek function to reposition the src_file file pointer to the end of the file + offset (note that this program takes the opposite number of offset, that is, the end of the file + (-offset)), and then starts copying the file forward from the end of the file + offset to dest_file.

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • Example explanation of alarm function in Linux
  • PHP executes 6 Linux command function code examples
  • Detailed explanation of the use of stat function and stat command in Linux
  • How to get the current time using time(NULL) function and localtime() in Linux
  • How to add a timeout to a Python function on Linux/Mac
  • Linux unlink function and how to delete files
  • A brief analysis of the function calling process under the ARM architecture

<<:  Example of how to implement local fuzzy search function in front-end JavaScript

>>:  How to quickly create tens of millions of test data in MySQL

Recommend

User needs lead to marketing-oriented design

<br />For each of our topics, the team will ...

Solve the problem that await does not work in forEach

1. Introduction A few days ago, I encountered a p...

How to elegantly implement the mobile login and registration module in vue3

Table of contents Preface Input box component lay...

JavaScript anti-shake and throttling explained

Table of contents Stabilization Throttling Summar...

How to add a column to a large MySQL table

The question is referenced from: https://www.zhih...

Implementation of Docker cross-host network (overlay)

1. Docker cross-host communication Docker cross-h...

Detailed explanation of Docker daemon security configuration items

Table of contents 1. Test environment 1.1 Install...

Notes on the MySQL database backup process

Today I looked at some things related to data bac...

Bug of Chinese input garbled characters in flex program Firefox

Chinese characters cannot be input in lower versio...

Summary of the dockerfile-maven-plugin usage guide

Table of contents pom configuration Setting.xml c...

Explain TypeScript enumeration types in detail

Table of contents 1. Digital Enumeration 2. Strin...

This article takes you to explore NULL in MySQL

Table of contents Preface NULL in MySQL 2 NULL oc...