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. };

  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. }

  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

struct stat结构体的详解和用法的更多相关文章

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

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

  2. inode结构体成员详解

    概述:inode译成中文就是索引节点,它用来存放档案及目录的基本信息,包含时间.档名.使用者及群组等.inode分为内存中的inode和文件系统中的inode,为了避免混淆,我们称前者为VFS ino ...

  3. Solidity的自定义结构体深入详解

    一.结构体定义 结构体,Solidity中的自定义类型.我们可以使用Solidity的关键字struct来进行自定义.结构体内可以包含字符串,整型等基本数据类型,以及数组,映射,结构体等复杂类型.数组 ...

  4. IPv4地址结构体sockaddr_in详解

    sockaddr_in结构体定义 struct sockaddr_in { sa_family_t sin_family; //地址族(Address Family) uint16_t sin_por ...

  5. struct socket结构体详解

    原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://weiguozhihui.blog.51cto.com/3060615/15852 ...

  6. Scala 深入浅出实战经典 第53讲:Scala中结构类型实战详解

    王家林亲授<DT大数据梦工厂>大数据实战视频 Scala 深入浅出实战经典(1-64讲)完整视频.PPT.代码下载:百度云盘:http://pan.baidu.com/s/1c0noOt6 ...

  7. struct ifreq结构体与ip,子网掩码,网关等信息

    总结一下,今天学习的关于通过socket,ioctl来获得ip,netmask等信息,其中很多内容参照了很多网上的信息,我会一一列出的 我用的这个函数,就是下面这个函数,其中的有一些全局变量,很好懂, ...

  8. Golang面向对象编程-struct(结构体)

    Golang面向对象编程-struct(结构体) 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.什么是面向对象编程 面向对象编程(Object Oriented Program ...

  9. struct timeval结构体 以及 gettimeofday()函数(转)

    struct timeval结构体 转载地址:http://blog.chinaunix.net/uid-20548989-id-2533161.html 该结构体是Linux系统中定义,struct ...

随机推荐

  1. JSON详解(转)

    JSON详解 JSON的全称是”JavaScript Object Notation”,意思是JavaScript对象表示法,它是一种基于文本,独立于语言的轻量级数据交换格式.XML也是一种数据交换格 ...

  2. Codeforces Round #360 (Div. 1) D. Dividing Kingdom II 并查集求奇偶元环

    D. Dividing Kingdom II   Long time ago, there was a great kingdom and it was being ruled by The Grea ...

  3. Sql Server REPLACE函数的使用

    REPLACE用第三个表达式替换第一个字符串表达式中出现的所有第二个给定字符串表达式. 语法REPLACE ( ''string_replace1'' , ''string_replace2'' , ...

  4. json学习系列(7)JSONBuilder的用法

    JSONBuilder可以向文件中写入写入json字符串.如下面的例子: public class Test { public static void main(String args[]) thro ...

  5. 排序+逆向思维 ACdream 1205 Disappeared Block

    题目传送门 /* 从大到小排序,逆向思维,从最后开始考虑,无后向性 每找到一个没被淹没的,对它左右的楼层查询是否它是孤立的,若是++,若不是-- 复杂度 O(n + m),还以为 O(n^2)吓得写了 ...

  6. BZOJ2190 [SDOI2008]仪仗队(欧拉函数)

    与HDU2841大同小异. 设左下角的点为(1,1),如果(1,1)->(x,y)和(1,1)->(x',y')向量平行,那只有在前面的能被看见.然后就是求x-1.y-1不互质的数对个数. ...

  7. TYVJ P1088 treat Label:鞭笞人的DP

    时间: 1000ms / 空间: 131072KiB / Java类名: Main 背景 广东汕头聿怀初中 Train#2 Problem2 描述 给出长度为N的数列{A_i},每次可以从最左边或者最 ...

  8. POST模拟百度登录和自动发帖

    这里用HttpClient发包模拟百度登录和发帖,验证码部分采用机器下载人工识别. 登陆百度的原理:1. 访问https://passport.baidu.com/v2/api/?getapi& ...

  9. CentOS下搭建使用gitlab 和tortoiseGit使用

    gitlab和github 一样很爽的一个东西 关于gitlab在CentOS下的安装方法地址参考: https://github.com/gitlabhq/gitlab-recipes/tree/m ...

  10. JBPM表达业务流程(流程定义语言)

    业务流程包括三部分: 活动 Activity / 节点 Node (有很多种,不同的类型有不同的功能,必须要有一个Start Activity) 连线 Transition / 转移 (从一个Acti ...