struct stat結構體的詳解和用法

轉載地址:http://blog.csdn.net/sd6264456/article/details/18362269

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

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

  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 篇原創文章 · 獲贊 5 · 訪問量 7萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章