文件属性函数stat/fstat/lstat
来源:互联网 发布:安卓微信数据恢复大师 编辑:程序博客网 时间:2024/05/16 18:25
stat,lstat,fstat1 函数都是获取文件(普通文件,目录,管道,socket,字符,块()的属性。函数原型#include <sys/stat.h>
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);连接文件描述命,获取文件属性。
其中
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; //伟建内容对应的块数量
};
应用案例
#include <stdio.h>
#include <time.h>
#include <sys/stat.h>
#include <unistd.h>
#include <sys/types.h>
#include <errno.h>
int main(int argc, char *argv[])
{
struct stat buf;
// 检查参数
if (argc != 2) {
printf("Usage: my_stat <filename>/n");
exit(0);
}
// 获取文件属性
if ( stat(argv[1], &buf) == -1 ) {
perror("stat:");
exit(1);
}
// 打印出文件属性
printf("device is: %d/n", buf.st_dev);
printf("inode is: %d/n", buf.st_ino);
printf("mode is: %o/n", buf.st_mode);
printf("number of hard links is: %d/n", buf.st_nlink);
printf("user ID of owner is: %d/n", buf.st_uid);
printf("group ID of owner is: %d/n", buf.st_gid);
printf("device type (if inode device) is: %d/n", buf.st_rdev);
printf("total size, in bytes is: %d/n", buf.st_size);
printf(" blocksize for filesystem I/O is: %d/n", buf.st_blksize);
printf("number of blocks allocated is: %d/n", buf.st_blocks);
printf("time of last access is: %s", ctime(&buf.st_atime));
printf("time of last modification is: %s", ctime(&buf.st_mtime));
printf("time of last change is: %s", ctime(&buf.st_ctime));
return 0;
}
- 文件属性函数stat/fstat/lstat
- stat/fstat/lstat函数
- stat, fstat, lstat 函数
- stat、fstat、lstat函数
- stat/fstat/lstat函数
- 文件属性之stat(),fstat(),lstat(),chown(),fchown(),lchown()函数
- Linux获取文件属性stat()、fstat()、lstat()函数实例学习
- 获取文件属性—stat、lstat、fstat
- stat,fstat以及lstat函数
- stat,fstat以及lstat函数
- stat,fstat以及lstat函数
- stat,lstat,fstat 函数说明
- stat、fstat和lstat函数
- stat,fstat以及lstat函数
- stat,fstat以及lstat函数
- stat、fstat和lstat函数
- 函数stat、fstat、fstatat、lstat
- Linux 获取文件属性 函数 stat, fstat, lstat 以及 stat 结构体
- 一个项目用到的sql语句
- DQL、DML、DDL、DCL的概念与区别
- C基础:memmove和memcpy的区别
- 信号完整性:PCB走线宽度变化产生的反射
- Oracle管理专题之:用于监控Session、process、Lock的几个SQL语句
- 文件属性函数stat/fstat/lstat
- C Run Time--crt0.o 分析
- 世博会==人多
- js给表格增加一行总结
- multiple definition of...
- 张卫健被胃癌,自我炒作还是微波账户被盗?娱乐圈被死亡事件大揭秘
- typeof & Nan & ==
- C# AppDomain.CurrentDomain.BaseDirectory
- 很有用的面试知识---面试100问(不仅仅是面试)