设为首页 加入收藏

TOP

Linux stat函数讲解(二)
2014-11-24 07:26:09 来源: 作者: 【 】 浏览:4
Tags:Linux stat 函数 讲解
truct stat{
mode_t st_mode; /*file tpye &mode (permissions)*/
ino_t st_ino; /*i=node number (serial number)*/
dev_t st_rdev; /*device number for special files*/
nlink_t st_nlink; /*number of links*/
uid_t st_uid; /*user id of owner*/
gid_t st_gid; /*group ID of owner*/
off_t st_size; /*size in bytes for regular files*/
time_t st_atime; /*time of last access*/
time_t st_mtime; /*time of last modification*/
time_t st_ctime; /*time of last file status change*/
long st_blksize; /*best I/O block size */
long st_blocks; /*number of 512-byte blocks allocated*/
};
注意,除最后两个以外,其他各成员都为基本系统数据类型。我们将说明此结构的每个成员以了解文件属性。

使用stat函数最多的可能是ls-l命令,用其可以获得有关一个文件的所有信息。
1 函数都是获取文件(普通文件,目录,管道,socket,字符,块()的属性。
函数原型
#include
int stat(const char *restrict pathname, struct stat *restrict buf);
提供文件名字,获取文件对应属性。
int fstat(int filedes, struct stat *buf);
通过文件描述符获取文件对应的属性。
int lstat(const char *restrict pathname, struct stat *restrict buf);
连接文件描述命,获取文件属性。
2 文件对应的属性
struct stat {
mode_t st_mode; //文件对应的模式,文件,目录等
ino_t st_ino; //inode节点号
dev_t st_dev; //设备号码
dev_t st_rdev; //特殊设备号码
nlink_t st_nlink; //文件的连接数
uid_t st_uid; //文件所有者
gid_t st_gid; //文件所有者对应的组
off_t st_size; //普通文件,对应的文件字节数
time_t st_atime; //文件最后被访问的时间
time_t st_mtime; //文件内容最后被修改的时间
time_t st_ctime; //文件状态改变时间
blksize_t st_blksize; //文件内容对应的块大小
blkcnt_t st_blocks; //伟建内容对应的块数量
};
可以通过上面提供的函数,返回一个结构体,保存着文件的信息。


首页 上一页 1 2 下一页 尾页 2/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇如何用C语言获取文件的大小 下一篇Linux内核container_of宏

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·Redis 分布式锁全解 (2025-12-25 17:19:51)
·SpringBoot 整合 Red (2025-12-25 17:19:48)
·MongoDB 索引 - 菜鸟 (2025-12-25 17:19:45)
·What Is Linux (2025-12-25 16:57:17)
·Linux小白必备:超全 (2025-12-25 16:57:14)