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