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

Linux system prohibits remote login command of root account

ps: Here is how to disable remote login of root a...

How to implement scheduled backup of MySQL in Linux

In actual projects, the database needs to be back...

JavaScript operation elements teach you how to change the page content style

Table of contents 1. Operation elements 1.1. Chan...

In-depth study of MySQL composite index

A composite index (also called a joint index) is ...

JS implements user registration interface function

This article example shares the specific code of ...

HTML table markup tutorial (48): CSS modified table

<br />Now let's take a look at how to cl...

Ways to improve MongoDB performance

MongoDB is a high-performance database, but in th...

MySQL 8.0.18 adds users to the database and grants permissions

1. It is preferred to use the root user to log in...

The use and difference between vue3 watch and watchEffect

1.watch listener Introducing watch import { ref, ...

Detailed explanation of the use of MySQL concatenation function CONCAT

The previous articles introduced the replacement ...

JavaScript design pattern learning adapter pattern

Table of contents Overview Code Implementation Su...

MySQL detailed single table add, delete, modify and query CRUD statements

MySQL add, delete, modify and query statements 1....