天天看點

通過stat和fstat取得檔案資訊

/*

* Linux檔案屬性結構體資訊

* struct stat

* {

* dev_t st_dev;//裝置

* ino_t st_ino;//結點

* mode_t st_mode;//模式

* nlink_t st_nlink;//硬連接配接

* uid_t st_uid;//使用者ID

* gid_t st_gid;//組ID

* dev_t st_rdev;//裝置類型

* off_t st_off;//檔案位元組數

* unsigned long st_blksize;//塊大小

* unsigned long st_blocks;//塊數

* time_t st_atime;//最後一次通路時間

* time_t st_mtime;//最後一次修改時間

* time_t st_ctime;//最後一次屬性改變時間

* }

* 可通過stat或fstat函數獲得檔案的其他屬性

* stat函數用來判斷沒有打開的檔案,而fstat函數用來判斷打開的檔案。

*

*/

#include <unistd.h>

#include <sys/stat.h>

int main(void)

{

struct stat buf;

stat("/etc/passwd",&buf);

/*

* stat函數

* 取得檔案的屬性

* sys/stat.h,unistd.h

* int stat(const char *file_name,struct stat*buf);

* 将參數file_name所指的檔案狀态複制到參數buf所指的結構中

* 成功傳回0,失敗傳回-1,錯誤存儲于errno

*/

printf("/etc/passwd檔案的大小是:%d/n",buf.st_size);

printf("/etc/passwd檔案的最後一次修改時間是:%d/n",buf.st_ctim);

return 0;

}

繼續閱讀