Welcome 微信登录

首页 / 操作系统 / Linux / 使用stat函数获取文件基本信息

函数原型: 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.       
  21.     stat(filename, &st);  
  22.     cout << "file name:" << filename << endl;  
  23.     cout << "file size:" << st.st_size << endl;  
  24.     cout << "file owner id:" << st.st_uid << endl;  
  25.     cout << "modify time:" << ctime(&st.st_mtime) << endl;  
  26.     cout << "created time:" << ctime(&st.st_ctime) << endl;  
  27.     return 0;  
  28. }  
程序运行结果: