[cpp] view plaincopy
  1. //! 需要包含de头文件
  2. #include <sys/types.h>
  3. #include <sys/stat.h>

S_ISLNK(st_mode):是否是一个连接.
S_ISREG(st_mode):是否是一个常规文件.
S_ISDIR(st_mode):是否是一个目录
S_ISCHR(st_mode):是否是一个字符设备.
S_ISBLK(st_mode):是否是一个块设备
S_ISFIFO(st_mode):是否 是一个FIFO文件.
S_ISSOCK(st_mode):是否是一个SOCKET文件

  1. int stat(const char *filename, struct stat *buf); //! prototype,原型
  2. struct stat
  3. {
  4. dev_t       st_dev;     /* ID of device containing file -文件所在设备的ID*/
  5. ino_t       st_ino;     /* inode number -inode节点号*/
  6. mode_t      st_mode;    /* protection -保护模式?*/
  7. nlink_t     st_nlink;   /* number of hard links -链向此文件的连接数(硬连接)*/
  8. uid_t       st_uid;     /* user ID of owner -user id*/
  9. gid_t       st_gid;     /* group ID of owner - group id*/
  10. dev_t       st_rdev;    /* device ID (if special file) -设备号,针对设备文件*/
  11. off_t       st_size;    /* total size, in bytes -文件大小,字节为单位*/
  12. blksize_t   st_blksize; /* blocksize for filesystem I/O -系统块的大小*/
  13. blkcnt_t    st_blocks;  /* number of blocks allocated -文件所占块数*/
  14. time_t      st_atime;   /* time of last access -最近存取时间*/
  15. time_t      st_mtime;   /* time of last modification -最近修改时间*/
  16. time_t      st_ctime;   /* time of last status change - */
  17. };
[cpp] view plaincopy
  1. #include <iostream>
  2. #include <ctime>
  3. #include <sys/types.h>
  4. #include <sys/stat.h>
  5. using namespace std;
  6. int
  7. main ()
  8. {
  9. struct stat buf;
  10. int result;
  11. result = stat ("./Makefile", &buf);
  12. if (result != 0)
  13. {
  14. perror ("Failed ^_^");
  15. }
  16. else
  17. {
  18. //! 文件的大小,字节为单位
  19. cout << "size of the file in bytes: " << buf.st_size << endl;
  20. //! 文件创建的时间
  21. cout << "time of creation of the file: " << ctime (&buf.st_ctime) <<
  22. endl;
  23. //! 最近一次修改的时间
  24. cout << "time of last modification of the file: " <<
  25. ctime (&buf.st_mtime) << endl;
  26. //! 最近一次访问的时间
  27. cout << "time of last access of the file: " << ctime (&buf.st_atime)
  28. << endl;
  29. }
  30. return 0;
  31. }
[cpp] view plaincopy
  1. $ ./test
  2. size of the file in bytes: 36
  3. time of creation of the file: Sun May 24 18:38:10 2009
  4. time of last modification of the file: Sun May 24 18:38:10 2009
  5. time of last access of the file: Sun May 24 18:38:13 2009

转载于:https://www.cnblogs.com/wxmdevelop/p/4649495.html

struct stat结构体的详解和用法相关推荐

  1. 结构体+联合体 详解

    文章目录 一.结构体 1.结构体变量 2.特殊声明 3.结构体的引用 1.嵌套调用 2.自引用 三.结构体的初始化 四.结构体的内存对齐 1.用法 2.练习题 3.修改对齐数 五.位段 1.用法 2. ...

  2. C语言指针结构体详解,结构体指针,C语言结构体指针详解

    结构体指针,可细分为指向结构体变量的指针和指向结构体数组的指针. 指向结构体变量的指针 前面我们通过"结构体变量名.成员名"的方式引用结构体变量中的成员,除了这种方法之外还可以使用 ...

  3. c语言结构体指针详解,结构体指针,C语言结构体指针详解

    结构体指针,可细分为指向结构体变量的指针和指向结构体数组的指针. 指向结构体变量的指针 前面我们通过"结构体变量名.成员名"的方式引用结构体变量中的成员,除了这种方法之外还可以使用 ...

  4. C语言——结构体(入门详解)

    结构体 文章目录 结构体 1.结构体的引出 2.结构体与结构体变量的关系示意图 3.**快速入门面向对象的方式(struct)解决养猫问题** 4.结构体和结构体变量的区别和联系 5.结构体变量在内存 ...

  5. C语言结构体对齐详解

    文章目录 一.C语言结构体对齐大小快速判断 二.反汇编角度看结构体 三.总结 一.C语言结构体对齐大小快速判断 在C语言中定义一个结构体,里面具体占用多少个字节呢,先举一个例子,如下: #includ ...

  6. c语言 一个函数返回结构体指针,详解C语言结构体中的函数指针

    结构体是由一系列具有相同类型或不同类型的数据构成的数据集合.所以,标准C中的结构体是不允许包含成员函数的,当然C++中的结构体对此进行了扩展.那么,我们在C语言的结构体中,只能通过定义函数指针的方式, ...

  7. 【C语言】结构体定义 typedef struct 用法详解和用法小结

    结构体定义 typedef struct 用法详解和用法小结 文章目录 结构体定义 typedef struct 用法详解和用法小结 0. 前言 1. 首先:在C中定义一个结构体类型要用typedef ...

  8. struct sk_buff结构体详解

    struct sk_buff是linux网络系统中的核心结构体,linux网络中的所有数据包的封装以及解封装都是在这个结构体的基础上进行. 1 2 3 4 5 6 7 8 9 10 11 12 13 ...

  9. linux sock结构体,struct socket结构体详解

    在内核中为什么要有struct socket结构体呢? struct socket结构体的作用是什么? 下面这个图,我觉得可以回答以上两个问题.  由这个图可知,内核中的进程可以通过使用struct ...

最新文章

  1. AppData::create pipe(2) failed: Too many open file
  2. printf 规定数据输出方式
  3. Docker - 导出导入容器
  4. linux jmeter 内存,JMeter-Linux内存不足
  5. lucene造成磁盘空间不足的问题
  6. 软件测试52讲-安全第一:渗透测试
  7. ASP.NET Core 中间件Diagnostics使用 异常和错误信息
  8. 31线性空间05——列空间和零空间、维数
  9. BIND9源码分析奠基
  10. Android Dialog的简单说明
  11. iostate知识要点
  12. 70多套java必练项目,适合小白上手!
  13. oracle学习札记95
  14. WinZip for Mac注册版
  15. WINDOWS2008server安全策略设置
  16. Bootstrap系列之面包屑导航(Breadcrumb)
  17. peopleSoft常见错误诊断
  18. Power BI中文版
  19. 如何在Commander One中显示隐藏文件?
  20. linux网卡的配置文件是双引号,Linux修改网卡ens33为eth0以及centos7下修改动态IP为静态IP地址...

热门文章

  1. VS2012代码提示快捷键
  2. 阿里云Centos 解决挖矿程序:kdevtmpfsi--服务器CPU占用高、内存占用高
  3. SpringBoot2.x 不反回空值属性
  4. 由于找不到PBSYS90.dll,无法继续执行代码。重新安装程序可能会解决此问题!
  5. 面试常碰到++p/p--问题到底结果是什么?
  6. iloc loc 区别
  7. Imagination发布四款RISC-V CPU
  8. 2021年大数据ELK(二十二):采集Apache Web服务器日志
  9. Androidx FloatingActionButton 中间图片颜色值修改
  10. Android Studio 引入aar文件