struct stat结构体的使用

struct stat结构体的使用

最近在操作文件时需要一次将一个文件加载到内存,需要提前得到文件的大小,了解到关系struct stat结构体的使用
struct stat主要描述文件书的结构
需要引入的头文件#include<sys/types.h>,#include<sys/stat.h>(后一个只在linux中引入) (linux 和Windows都可以)

// 在windows中
// static __inline int __CRTDECL stat(const char * _Filename, struct stat * _Stat);

struct stat {
        _dev_t     st_dev;              // 文件(目录)的驱动器号
        _ino_t     st_ino;              // 节点号(索引节点)
        unsigned short st_mode;         // 文件类型或者模式
        short      st_nlink;            // 文件的硬连接计数(多个文件名指向一个节点号)
        short      st_uid;              // 文件的用户ID (创建者ID)
        short      st_gid;              // 文件的组ID
        _dev_t     st_rdev;             // 设备号(如果文件为设备文件)
        _off_t     st_size;             // 文件大小(字节数)
        time_t st_atime;                // 最近存取(访问)时间(access)(取用文件内容时改变)
        time_t st_mtime;                // 最近修改时间(modification)(文件内容更改会改变)
        time_t st_ctime;                // 状态时间(change)(更改权限或者属性会改变)
        };

// Linux
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
};

// 高精度时间的一个结构体
struct timespec {
time_t tv_sec; // seconds 
long tv_nsec; // and nanoseconds 
};

S_ISLNK (st_mode)    判断是否为符号连接
S_ISREG (st_mode)    是否为一般文件
S_ISDIR (st_mode)    是否为目录
S_ISCHR (st_mode)    是否为字符装置文件
S_ISBLK (s3e)        是否为先进先出
S_ISSOCK (st_mode)   是否为socket
  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值