Linux

关注公众号 jb51net

关闭
首页 > 网站技巧 > 服务器 > Linux > linux lseek函数移动文件指针与获取文件长度

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

作者:妙缘12138

想知道lseek函数怎么用吗?本文详细讲解lseek函数的功能、参数和返回值,包括如何移动文件指针、获取当前文件指针位置、获取文件长度以及拓展文件长度等实战技巧,帮助你深入理解Linux系统中的文件偏移量操作

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

具体输入命令:

man 2 lseek

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

lseek函数

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

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

知道了这个概念了后,我们就了解了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帮助文档,开头有些查阅方法,一手知识永远是最好的知识。

whence :

再来聊一下返回值:

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函数的功能还挺好玩的。

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

您可能感兴趣的文章:
阅读全文