首页 > 系统相关 >Linux函数之lseek、stat、lstat

Linux函数之lseek、stat、lstat

时间:2023-03-27 10:37:24浏览次数:36  
标签:lseek stat lstat int fd include SEEK

Linux函数之lseek、stat、lstat的简单介绍

lseek函数

在Linux终端下输入命令:man 2 lseek,可以查看具体函数信息

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

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

    参数:
        - fd:文件描述符:通过open得到,用于操作文件
        - offset:偏移量
        - whence:
            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.
    
    返回值:
        返回文件指针的位置
    作用:
        1. 移动文件指针到头文件        lseek(fd,0,SEEK_SET);
        2. 获取文件当前指针的位置      lseek(fd,0,SEEK_CUR);
        3. 获取文件的长度             lseek(fd,0,SEEK_END);
        4. 拓展文件的长度             lseek(fd,100,SEEK_END);

下面为简单案例:

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

int main(){

    int fd = open("./hello.txt",O_WRONLY);
    if(fd == -1){
        perror("open");
        return -1;

    }

    //扩展文件
    int ret = lseek(fd,100,SEEK_END);
    if(ret == -1){
        perror("lseek");
        return -1;
    }

    //写入空数据
    write(fd,"mengyan",7);

    lseek(fd,12,SEEK_SET);
    write(fd,"miaomiao",8);

    //关闭文件
    close(fd);
    

    return 0;
}

stat函数

在Linux终端下输入命令:man 2 stat,可以查看具体函数信息

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

        int stat(const char *pathname, struct stat *statbuf);
            作用:获取一个文件的相关信息
            参数:
                - pathname:操作文件的路径
                - statbuf:结构体变量,传出参数,用于保存获取到的文件信息
            返回值:
                - 成功:返回0
                - 失败:返回-1,设置error

        int lstat(const char *pathname, struct stat *statbuf);
            作用:用于获取连接文件的信息

下面为简单案例:

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <iostream>

int main(){

    struct stat statbuf;
    int ret = stat("./hello.txt",&statbuf);
    if(ret == -1){
        perror("stat");
        return -1;
    }

    std::cout << statbuf.st_size <<std::endl;

    return 0;
}

lstat函数

作用:用于获取链接文件的信息

标签:lseek,stat,lstat,int,fd,include,SEEK
From: https://www.cnblogs.com/nakjima/p/17258717.html

相关文章

  • C-静态static修饰符
    静态static修饰符局部变量普通局部变量:在任何一个函数内部定义的变量(不加static修饰符)都属于这个范畴.它的值在初始时是不确定的,除非对其进行初始化.普通局部变量......
  • stata:合并merge时如何处理关键变量外的同名变量的值分三种情况
    useceshi1,clearlist////+------------------------------------------------------+//|idks2019ks2020ks2021ks2022ks2023ab|//......
  • Vue——initState【十】
    前言前面我们简单的了解了vue初始化时的一些大概的流程,这里我们详细的了解下具体的内容;内容这一块主要围绕init.ts中的initState进行剖析,初始化生命周期之后紧接着。......
  • What is static and dynamic libraries
    Whatisstaticanddynamiclibraries他们有什么相同点吗?都是库文件。对于调用库文件的使用者来说,不管是静态库还是动态库,调用的方式都是一样的,没什么区别。Difference......
  • Why is redux state immutable???
    众所周知,redux的三项原则之一有stateisread-only,即immutable.为了保证immutable,所以每次reducer都要return一个newobject,作为新的state.但为什么state一定要......
  • vue状态管理pinia之监听state里的对象
    如上图所示,我需要监听layerList的变化,代码如下:importuseLayersStorefrom"@/store/modules/layers";constlayersStore=useLayersStore();watch(()=>userStore.l......
  • how to set static ip using command line for kali linux
    HowtoconfigureKaliLinuxtouseastaticIPaddresshttps://miloserdov.org/?p=542  sudovim/etc/network/interfaces  autoeth0ifaceeth0inets......
  • STAT802 分析
    STAT802–Assignment1,PartA.1STAT802:AdvancedTopicsinAnalytics-Semester12023STAT802Assignment1–PartADue:5pmonFriday24March2023Outlin......
  • StatefulSet 更新策略
    OnDelete策略OnDelete更新策略实现了传统(1.7版本之前)的行为,它也是默认的更新策略。当我们选择这个更新策略并修改StatefulSet的.spec.template字段时,StatefulSet控......
  • StatefulSet 扩容和缩容
    概念和Deployment类似,可以通过更新replicas字段扩容/缩容StatefulSet,也可以使用kubectlscale、kubectleditkuectlpatch来扩容/缩容一个StatefulSet扩容kube......