Domin1c0's Blog

A technical blog sharing my journey in programming, development, and open source.

View on GitHub
22 July 2025

lseek

by

lseek 函数

移动文件读写位置(偏移指针)

函数原型:

参数:

返回值:

从开头写入数据

int fd = open("file.txt", O_RDWR);
lseek(fd, 0, SEEK_SET); //移动文件指针到开头
write(fd, "ABC", 3);    //从开头写入数据

获取文件大小

int fd = open("file.txt", O_RDONLY);
off_t size = lseek(fd, 0, SEEK_END);  //从末尾偏移0,返回即是文件大小
tags: 进程 - 进程相关函数