函数stat、fstat、fstatat和lstat
一、主要函数应用
#include <sys/stat.h>
int stat(const char *path, struct stat *buf);
int fstat(int fd, struct stat *buf)
int lstat(const char *path, struct stat *buf);
int fstat(int fd, const char *path, struct stat *buf, int flag);
参数:
- stat读取的是连接文件指向的文件的属性l
- stat读取的链接文件本身的属性lstat
二、struct stat 的结构体
struct stat
{dev_t st_dev; //文件的设备编号ino_t st_ino; //节点mode_t st_mode; //文件的类型和存取的权限nlink_t st_nlink; //连到该文件的硬连接数目,刚建立的文件值为1uid_t st_uid; //用户IDgid_t st_gid; //组IDdev_t st_rdev; //(设备类型)若此文件为设备文件,则为其设备编号off_t st_size; //文件字节数(文件大小)blksize_t st_blksize; //块大小(文件系统的I/O 缓冲区大小)blkcnt_t st_blocks; //块数time_t st_atime; //最后一次访问时间time_t st_mtime; //最后一次修改时间time_t st_ctime; //最后一次改变时间(指属性)
};
三、st_mode 的结构
- 15-12 位保存文件类型
- 11-9 位保存执行文件时设置的信息
- 8-0 位保存文件访问权限
图1 展示了 st_mode 各个位的结构。
四、一些常用的宏
-st_mode -- 16位整数○ 0-2 bit -- 其他人权限
- S_IROTH 00004 读权限
- S_IWOTH 00002 写权限
- S_IXOTH 00001 执行权限
- S_IRWXO 00007 掩码, 过滤 st_mode中除其他人权限以外的信息○ 3-5 bit -- 所属组权限
- S_IRGRP 00040 读权限
- S_IWGRP 00020 写权限
- S_IXGRP 00010 执行权限
- S_IRWXG 00070 掩码, 过滤 st_mode中除所属组权限以外的信息○ 6-8 bit -- 文件所有者权限
- S_IWUSR 00200 写权限
- S_IXUSR 00100 执行权限
- S_IRWXU 00700 掩码, 过滤 st_mode中除文件所有者权限以外的信息○ 12-15 bit -- 文件类型
- S_IFSOCK 0140000 套接字
- S_IFLNK 0120000 符号链接(软链接)
- S_IFBLK 0060000 块设备
- S_IFDIR 0040000 目录
- S_IFCHR 0020000 字符设备
- S_IFIFO 0010000 管道
- S_IFMT 0170000 掩码,过滤 st_mode中除文件类型以外的信息(st_mode & S_IFMT) == S_IFREG
五、程序清单
测试代码:
#include <stdio.h>
#include <sys/stat.h>
#include <stdlib.h>int main()
{struct stat st;struct stat* st1;st1 = &st;int ret = stat("english.txt", &st);if(ret == -1) {perror("stat error");exit(1);}printf("file size = %d\n", (int)st.st_size);return 0;
}
输出结果:
#include <stdio.h>
#include <sys/stat.h>
#include <stdlib.h>int main()
{struct stat st;struct stat* st1;st1 = &st;int ret = stat("english.txt", &st);if(ret == -1) {perror("stat error");exit(1);}printf("file size = %d\n", (int)st.st_size);if(st.st_mode & S_IFMT == S_IFREG) {printf("这是一个普通文件\n");}if(st.st_mode & S_IRUSR) {printf(" R ");}if(st.st_mode & S_IWUSR) {printf(" W ");}if(st.st_mode & S_IXUSR) {printf(" X ");}printf("\n");return 0;
}
输出结果:
#include <stdio.h>
#include <sys/stat.h>
#include <stdlib.h>int main()
{struct stat st;struct stat* st1;st1 = &st;int ret = lstat("s.s", &st);if(ret == -1) {perror("stat error");exit(1);}printf("file size = %d\n", (int)st.st_size);if(st.st_mode & S_IFMT == S_IFREG) {printf("这是一个普通文件\n");}if(st.st_mode & S_IRUSR) {printf(" R ");}if(st.st_mode & S_IWUSR) {printf(" W ");}if(st.st_mode & S_IXUSR) {printf(" X ");}printf("\n");return 0;
}
输出结果:
参考资料:
1. 12-stat 函数
2. 13-stat 结构体 st_mode 字段
函数stat、fstat、fstatat和lstat相关推荐
- UNIX 环境高级编程 文件和目录
函数stat , fstat , fstatat , lstat stat函数返回与此文件有关的信息结构. fstat函数使用已打开的文件描述符(而stat则使用文件名) fstatat函数 为一个相 ...
- Linux中fstat、stat和lstat的用法与区别
stat系统调用系列包括了fstat.stat和lstat,它们都是用来返回"相关文件状态信息"的,三者的不同之处在于设定源文件的方式不同. struct stat结构体 首先隆重 ...
- linux下fstat、stat和lstat 区别(转)
原文地址::http://www.cnitblog.com/guopingleee/archive/2008/11/13/51411.aspx 相关文章 1.C语言中fstat.stat和lstat ...
- linux文件操作函数程序,linux 文件操作函数
文件操作(Linux) 常用linux函数库中的基本函数 1. close函数,关闭文件 2. creat函数,建立文件 3. dup.dup2函数,复制文件描述词 4. fcntl函数,文件描述词操 ...
- linux time函数_Linux基础知识(三)
本篇介绍一些Linux文件操作API 函数的使用. 1.基本概念 1.1 linux 下一切皆文件(网络设备除外) (1) 普通文件 (2) 目录 (3) 硬件设备 - 控制台 /dev/consol ...
- Linux 常用C函数说明-文件权限控制篇
chdir(改变当前的工作(目录) 相关函数 getcwd,chroot 表头文件 #include<unistd.h> 定义函数 int chdir(const char * pa ...
- linux常用c函数(中文版)
都是linux的c函数东西略多,用页面搜索来查找吧. << Back to man.ChinaUnix.net isalnum(测试字符是否为英文或数字) 相关函数 isalpha,isd ...
- linux中stat函数的用法,os.stat()函数的用法
os.stat() 函数会给出一个文件或文件描述符(file descriptor)的各种状态信息,如权限,大小,所属用户和组,修改时间等.这个函数实际上是调用OS的系统调用stat()来实现功能,对 ...
- C语言程序设计(常用函数说明)
C语言程序设计(常用函数说明) C 语言是1972年由美国的Dennis Ritchie设计发明的,并首次在UNIX操作系统的 DEC PDP-11计算机上使用.它由早期的编程语言 BCPL(Basi ...
最新文章
- ubuntu18 安装python3.8.tgz
- c语言如何持续输入字符直到指定字符结束_《小白学C》第三章 常用输入输出函数...
- 手机网页 右边的空白区
- 拼接图像亮度均匀调整_液晶拼接屏如何才能达到更好的显示效果
- java提高篇(四)---LinkedList
- react控制元素的显示或隐藏
- 解决<style>无法重写.css文件的问题
- python输入一个整数列表 列表元素为18_Python-18 (高级变量1--列表)
- java环境变量一闪而过_Java环境变量配置和Tomcat启动时cmd界面一闪而过问题
- [IOS]开源库RegexKitLite正则表达式的使用
- C#图片处理之: 锐化
- 网站关键词编写方法,注意事项。
- 万用表测试软件,最全的万用表使用方法看这里!
- 无法创建视频捕捉过滤器的解决办法
- Millet谷仓对电商的三大革命
- 如何安装cadence 软件
- 旅游订票订酒店团购(APP,JAVA后台管理,MYSQL)
- 字节抖音电商NLP算法一面
- 物理五大信道浅聊PRACH、PUCCH、PUSCH、PDCCH、PDSCH
- 真实评测:华为nova8和红米k30至尊版哪个好-参数区别对比
热门文章
- iOS:图片上传时两种图片压缩方式的比较
- android 处理鼠标滚轮事件 【转】
- C++细节系列(零):零散记录
- 排序算法(1) 快速排序 C++实现
- silverligh的数据访问
- 工作总结:文件对话框的分类(C++)
- 压缩图片上传到数据库
- arduino定时器函数如何使用_excel如何使用函数公式来查找图片
- Matlab看跌期权二叉树,欧式期权二叉树MATLAB程序
- 小票上为啥指甲能划出印_指甲上出现竖纹,除遗传问题,或是身体在向你拉警报了,别忽视...