歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> 使用stat函數獲取文件基本信息

使用stat函數獲取文件基本信息

日期:2017/3/1 11:10:55   编辑:Linux編程

函數原型: int stat(const char *pathname, struct stat *buf);

函數說明: 給stat函數傳遞一個pathname,stat函數返回一個與此命名文件有關的信息結構,該信息結構中包含文件的基本信息。

  1. //statdemo.cc
  2. #include <iostream>
  3. #include <ctime>
  4. #include <cstring>
  5. #include <unistd.h>
  6. #include <sys/types.h>
  7. #include <sys/stat.h>
  8. using namespace std;
  9. /**********************
  10. *利用stat函數獲取某個文件的相關信息
  11. *創建時間:2011.07.25
  12. *修改時間:2011.07.25
  13. *作者:hahaya
  14. ** ********************/
  15. int main()
  16. {
  17. const char *filename = "./hahaya.txt";
  18. struct stat st;
  19. memset(&st, 0, sizeof(st));
  20. stat(filename, &st);
  21. cout << "file name:" << filename << endl;
  22. cout << "file size:" << st.st_size << endl;
  23. cout << "file owner id:" << st.st_uid << endl;
  24. cout << "modify time:" << ctime(&st.st_mtime) << endl;
  25. cout << "created time:" << ctime(&st.st_ctime) << endl;
  26. return 0;
  27. }

程序運行結果:


Copyright © Linux教程網 All Rights Reserved