轉載地址:http://blog.csdn.net/sd6264456/article/details/18362269
- //! 需要包含de頭文件
- #include <sys/types.h>
- #include <sys/stat.h>
- int stat(const char *filename, struct stat *buf); //! prototype,原型
- struct stat
- {
- dev_t st_dev; /* ID of device containing file -文件所在設備的ID*/
- ino_t st_ino; /* inode number -inode節點號*/
- mode_t st_mode; /* protection -保護模式?*/
- nlink_t st_nlink; /* number of hard links -鏈向此文件的連接數(硬連接)*/
- uid_t st_uid; /* user ID of owner -user id*/
- gid_t st_gid; /* group ID of owner - group id*/
- dev_t st_rdev; /* device ID (if special file) -設備號,針對設備文件*/
- off_t st_size; /* total size, in bytes -文件大小,字節爲單位*/
- blksize_t st_blksize; /* blocksize for filesystem I/O -系統塊的大小*/
- blkcnt_t st_blocks; /* number of blocks allocated -文件所佔塊數*/
- time_t st_atime; /* time of last access -最近存取時間*/
- time_t st_mtime; /* time of last modification -最近修改時間*/
- time_t st_ctime; /* time of last status change - */
- };
- #include <iostream>
- #include <ctime>
- #include <sys/types.h>
- #include <sys/stat.h>
- using namespace std;
- int
- main ()
- {
- struct stat buf;
- int result;
- result = stat ("./Makefile", &buf);
- if (result != 0)
- {
- perror ("Failed ^_^");
- }
- else
- {
- //! 文件的大小,字節爲單位
- cout << "size of the file in bytes: " << buf.st_size << endl;
- //! 文件創建的時間
- cout << "time of creation of the file: " << ctime (&buf.st_ctime) <<
- endl;
- //! 最近一次修改的時間
- cout << "time of last modification of the file: " <<
- ctime (&buf.st_mtime) << endl;
- //! 最近一次訪問的時間
- cout << "time of last access of the file: " << ctime (&buf.st_atime)
- << endl;
- }
- return 0;
- }
- $ ./test
- size of the file in bytes: 36
- time of creation of the file: Sun May 24 18:38:10 2009
- time of last modification of the file: Sun May 24 18:38:10 2009
- time of last access of the file: Sun May 24 18:38:13 2009