struct stat结构体的详解和用法

转载地址:http://blog.csdn.net/sd6264456/article/details/18362269

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. //! 需要包含de头文件  
  2.   
  3. #include <sys/types.h>  
  4.   
  5. #include <sys/stat.h>   
  6.   
  7. int stat(const char *filename, struct stat *buf); //! prototype,原型   
  8.   
  9. struct stat  
  10. {  
  11.   
  12.     dev_t       st_dev;     /* ID of device containing file -文件所在设备的ID*/  
  13.   
  14.     ino_t       st_ino;     /* inode number -inode节点号*/  
  15.   
  16.     mode_t      st_mode;    /* protection -保护模式?*/  
  17.   
  18.     nlink_t     st_nlink;   /* number of hard links -链向此文件的连接数(硬连接)*/  
  19.   
  20.     uid_t       st_uid;     /* user ID of owner -user id*/  
  21.   
  22.     gid_t       st_gid;     /* group ID of owner - group id*/  
  23.   
  24.     dev_t       st_rdev;    /* device ID (if special file) -设备号,针对设备文件*/  
  25.   
  26.     off_t       st_size;    /* total size, in bytes -文件大小,字节为单位*/  
  27.   
  28.     blksize_t   st_blksize; /* blocksize for filesystem I/O -系统块的大小*/  
  29.   
  30.     blkcnt_t    st_blocks;  /* number of blocks allocated -文件所占块数*/  
  31.   
  32.     time_t      st_atime;   /* time of last access -最近存取时间*/  
  33.   
  34.     time_t      st_mtime;   /* time of last modification -最近修改时间*/  
  35.   
  36.     time_t      st_ctime;   /* time of last status change - */  
  37.   
  38. };  

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. #include <iostream>  
  2.   
  3. #include <ctime>  
  4.   
  5. #include <sys/types.h>  
  6.   
  7. #include <sys/stat.h>   
  8.   
  9. using namespace std;   
  10.   
  11. int  
  12. main ()  
  13. {  
  14.     struct stat buf;  
  15.   
  16.     int result;  
  17.   
  18.     result = stat ("./Makefile", &buf);  
  19.   
  20.     if (result != 0)  
  21.       {  
  22.           perror ("Failed ^_^");  
  23.       }  
  24.     else  
  25.       {  
  26.   
  27.           //! 文件的大小,字节为单位  
  28.   
  29.           cout << "size of the file in bytes: " << buf.st_size << endl;  
  30.   
  31.           //! 文件创建的时间  
  32.   
  33.           cout << "time of creation of the file: " << ctime (&buf.st_ctime) <<  
  34.   
  35.               endl;  
  36.   
  37.           //! 最近一次修改的时间  
  38.   
  39.           cout << "time of last modification of the file: " <<  
  40.   
  41.               ctime (&buf.st_mtime) << endl;  
  42.   
  43.           //! 最近一次访问的时间  
  44.   
  45.           cout << "time of last access of the file: " << ctime (&buf.st_atime)  
  46.   
  47.               << endl;  
  48.       }  
  49.   
  50.     return 0;  
  51.   
  52. }  

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. $ ./test  
  2.   
  3. size of the file in bytes: 36  
  4.   
  5. time of creation of the file: Sun May 24 18:38:10 2009  
  6.   
  7. time of last modification of the file: Sun May 24 18:38:10 2009  
  8.   
  9. time of last access of the file: Sun May 24 18:38:13 2009  
  10.    

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值