手机版
你好,游客 登录 注册 搜索
背景:
阅读新闻

使用stat函数获取文件基本信息

[日期:2011-10-24] 来源:Linux社区  作者:qiurisuixiang [字体:大 中 小]

函数原型: 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. }  

程序运行结果:


linux
相关资讯       Linux函数 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款