• C/C++获取文件大小


    获取文件大小

    概述

    获取文件大小的方法有两种,一种是利用文件头和文件尾的偏移字节数来得到文件大小,另一种是利用文件状态信息中记录的文件总字节数来获取文件大小。利用 fseek()ftell() 函数可以计算出一个文件的大小。fseek() 函数用于设置文件指针 stream 的位置,ftell() 函数用于得到文件位置指针当前位置相对于文件首的偏移字节数。利用 stat() 函数可以获取文件的状态信息,包括文件大小,文件类型等。

    使用偏移量获取文件大小

    语法

    #include 
    
    int fseek(FILE *stream, long offset, int whence);
    long ftell(FILE *stream);
    
    • 1
    • 2
    • 3
    • 4

    函数说明

    • stream – 指向 FILE 对象的指针
    • offset – 相对 whence 的偏移量,以字节为单位(正数或负数)
    • whence – 设定从文件的哪里开始偏移。取值为:SEEK_SET(文件开头)、SEEK_CUR(当前位置)、SEEK_END(文件结尾)。

    返回值

    • 如果成功,则该函数返回零,否则返回非零值。

    示例

    #include 
    
    int main(int argc, char const *argv[]) {
        long size = 0;
        FILE *fp = fopen("/usr/bin/ls", "r");
        fseek(fp, 0, SEEK_END);
        size = ftell(fp);
        printf("size: %ld\n", size);
        /* fseek(fp, 0, SEEK_SET); */
        fclose(fp);
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 第 5 行以只读形式打开 /usr/bin/ls 文件
    • 第 6 行调用 fseek() 函数并指向文件结尾
    • 第 7 行计算当前位置相对于文件首的偏移字节数,即得到文件大小值
    • 第 8 行打印文件大小
    • 第 9 行在该示例中注释掉了,当打开注释,指向文件开头,可以执行其他文件读写操作
    • 第 10 行使用完之后及时关闭文件指针

    使用 stat 获取文件大小

    stat() 函数获取的是文件的状态信息,不仅仅包含文件大小,还包含文件类型,用户 ID 等信息;该函数返回一个 struct stat 结构体,里面包含以下成员:

    struct stat {
        dev_t     st_dev;         /* ID of device containing file */
        ino_t     st_ino;         /* Inode number */
        mode_t    st_mode;        /* File type and mode */
        nlink_t   st_nlink;       /* Number of hard links */
        uid_t     st_uid;         /* User ID of owner */
        gid_t     st_gid;         /* Group ID of owner */
        dev_t     st_rdev;        /* Device ID (if special file) */
        off_t     st_size;        /* Total size, in bytes */
        blksize_t st_blksize;     /* Block size for filesystem I/O */
        blkcnt_t  st_blocks;      /* Number of 512B blocks allocated */
    
        /* Since Linux 2.6, the kernel supports nanosecond
            precision for the following timestamp fields.
            For the details before Linux 2.6, see NOTES. */
    
        struct timespec st_atim;  /* Time of last access */
        struct timespec st_mtim;  /* Time of last modification */
        struct timespec st_ctim;  /* Time of last status change */
    
    #define st_atime st_atim.tv_sec      /* Backward compatibility */
    #define st_mtime st_mtim.tv_sec
    #define st_ctime st_ctim.tv_sec
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24

    文件大小信息保存在 st_size 变量中,以字节为单位。

    语法

    #include 
    int stat(const char *pathname, struct stat *statbuf);
    
    • 1
    • 2

    函数说明

    • pathname – 路径字符串
    • statbuf – 文件状态信息结构体 struct stat 的对象指针

    返回值

    • 如果成功返回 0;
    • 如果错误返回 -1,同时记录在 errno 中,可以通过 strerror() 函数获取错误码的字符串。

    示例

    #include 
    #include    // included for stat()
    
    int main(int argc, char const *argv[]) {
        struct stat statbuf;
        stat("/usr/bin/ls", &statbuf);
        printf("size: %ld\n", statbuf.st_size);
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 第 5 行定义文件状态结构体变量 statbuf
    • 第 6 行调用 stat() 函数获取文件状态,结果存储在 statbuf 变量中
    • 第 7 行打印实际的文件大小值 statbuf.st_size
  • 相关阅读:
    R 语言 Hitters 数据分析
    ES6中的箭头函数详细梳理
    等级测评是什么意思?工作流程包含哪些?
    Seata AT模式下的源码解析(三)
    设计模式之 delegate 委托模式:Swift 实现
    scp/sftp/curl/sh
    react如何阻止父容器滚动
    C 语言实例 - 字符转 ASCII 码
    4、SpringBoot_Mybatis、Druid、Juint整合
    赋能中国制造,大道云行发布智能制造分布式存储解决方案
  • 原文地址:https://blog.csdn.net/bluebird_shao/article/details/126248309