当前位置: 代码网 > 服务器>服务器>Linux > linux lseek函数详解:如何移动文件指针与获取文件长度

linux lseek函数详解:如何移动文件指针与获取文件长度

2026年09月06日 Linux 我要评论
如果,想要深入的学习linux系统调用函数lseek了话,还是需要去阅读linux系统中的帮助文档的。具体输入命令:man 2 lseek即可查阅到完整的资料信息。lseek函数lseek函数是lin

如果,想要深入的学习linux系统调用函数lseek了话,还是需要去阅读linux系统中的帮助文档的。

具体输入命令:

man 2 lseek

即可查阅到完整的资料信息。

lseek函数

lseek函数是linux系统api中的一员,它的官方定义是:重新定位读或写的文件偏移量。

这里科普一下什么叫做当前文件偏移量:

  • 每当打开一个文件,都会有一个叫做“当前文件偏移量”的东西,如果难理解也可以将他理解为指针。 除非打开文件时指定o_append选项,否则文件偏移量默认设置为0
  • 当我们发生了一次读或者写操作时,都会使这个当前文件偏移量发生变化,读/写多少字节,当前偏移量就会向后移动多少。

知道了这个概念了后,我们就了解了lseek函数它有些什么作用。下面我们来细细介绍一下这个函数。

它的函数原型是长这样的:

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

先来说一下这个off_t类型吧,它用于指示文件的偏移量。你可以就简单的理解为这是一个64位的整形数,相当于long long int,其定义在unistd.h头文件中可以查看。

在使用这个函数之前,我们需要往c/c++文件中导入这些头文件:

#include <sys/types.h>
#include <unistd.h>

通过lseek函数的函数原型我们可以知道,我们需要给它传入3个参数,那我们依次介绍这三个参数是什么,有什么含义在里面。

ps:题外话,如果有听不懂的地方,请自行查阅linux帮助文档,开头有些查阅方法,一手知识永远是最好的知识。

  • 参数:fd //文件描述符,可以通过open函数得到,通过这个fd可以操作某个文件
  • 参数: offset //文件偏移量,是一个整形数
  • 参数:whence //偏移类型,下列三个值中选一个。

whence :

  • seek_set:该文件的偏移量设为离文件开头offset个字节.
  • seek_cur:该文件的偏移量设为其当前值加offset(ps :offest可正负).
  • seek_end:该文件的偏移量设为文件长度加offset

再来聊一下返回值:

  • 如果成功:返回文件指针的位置
  • 如果失败:返回-1,并将错误原因赋值给errno,我们可以用标准c库中的perror函数打印出错误原因。需要引入头文件 #include <stdio.h>

lseek函数的作用

lseek函数的作用有以下四点:

1.移动文件指针到文件头:

lseek(fd, 0, seek_set);

2.获取当前文件指针的位置

lseek(fd, 0, seek_cur);

3.获取文件长度

lseek(fd, 0, seek_end);

4.拓展文件的长度,当前文件10b, 110b, 增加了100个字节

lseek(fd, 100, seek_end)

注意:拓展完需要再写一次数据,否则拓展无效

光这样介绍不可能学的会,我们来通过一个实战例子来彻底了解一下这个lseek函数

实战演练:lseek函数

作用1:移动文件指针到文件头:

//导入所有需要的头文件
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>

//我们的目的是:移动文件指针到文件头:
int main()
{
    //获取文件的文件描述符
    int fd = open("text.txt", o_rdwr);
    if (fd == -1)
    {
        perror("open");
        return -1;
    }

    //输出当前文件的偏移量
    long long int loc = lseek(fd, 0, seek_cur);
    printf("%lld\n", loc);

    //使用read函数读3个字节的数据
    char buf[3] = {0};
    int rnum = read(fd, buf, sizeof(buf));
    printf("%d\n", rnum);

    //再次查看文件的偏移量
    long long int loc1 = lseek(fd, 0, seek_cur);
    printf("%lld\n", loc1);

        //移动文件指针到文件头
    long long int loc2 = lseek(fd, 0, seek_set);
    printf("%lld\n", loc2);

    return 0;
}

作用2:获取当前文件指针的位置

//导入所有需要的头文件
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>

int main()
{
    //导入所有需要的头文件
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>

int main()
{
    //获取文件的文件描述符
    int fd = open("hello.txt", o_rdwr);

    //输出当前文件的偏移量
    long long int loc = lseek(fd, 0, seek_cur);
    printf("%lld\n", loc);

    //使用read函数读2个字节的数据
    char buf[2] = {0};
    int rnum = read(fd, buf, sizeof(buf));
    printf("%d\n", rnum);

        //再次查看文件的偏移量
    long long int loc1 = lseek(fd, 0, seek_cur);
    printf("%lld\n", loc1);

    return 0;
}
}

作用3:获取文件长度

//导入所有需要的头文件
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>

int main()
{
    //获取文件的文件描述符
    int fd = open("hello.txt", o_rdwr);


    //获取文件长度
    long long int loc1 = lseek(fd, 0, seek_end);
    printf("%lld\n", loc1);

    return 0;
}

作用4:拓展文件的长度(注:拓展完需要再写一次数据,否则拓展无效)

//导入所有需要的头文件
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>

int main()
{
    //获取文件的文件描述符
    int fd = open("hello.txt", o_rdwr);


    //获取文件长度
    long long int loc1 = lseek(fd, 0, seek_end);
    printf("%lld\n", loc1);

    //拓展文件的长度
    long long int loc2 = lseek(fd, 100, seek_end);
    write(fd," ",1);//写入一个空数据
    printf("%lld\n", loc2);


    return 0;
}

总结

当明白了当前文件偏移量这个概念了以后,lseek函数也变的并不是那么难理解了。仔细试了一下lseek函数的功能还挺好玩的。

以上为个人经验,希望能给大家一个参考,也希望大家多多支持代码网。

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2026  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com