Chinaunix首页 | 论坛 | 博客
  • 博客访问: 161785
  • 博文数量: 76
  • 博客积分: 1513
  • 博客等级: 上尉
  • 技术积分: 755
  • 用 户 组: 普通用户
  • 注册时间: 2011-11-25 15:15
文章分类

全部博文(76)

文章存档

2012年(2)

2011年(74)

我的朋友

分类: C/C++

2011-11-25 21:18:13

  1. #include<sys/stat.h>

  2. #include<iostream>

  3. #include<ctime>

  4. using namespace std;

  5.  

  6. int main()

  7. {

  8.     struct stat buf;

  9.     int result;

  10.     result=stat("1.mp3",&buf);//stat()函数获取文件状态,成功返回0,失败返回-1。

  11.     if(result!=0)

  12.                  perror("Failed !");

  13.     else

  14.     {

  15.         cout<<"size of the file in bytes:"<<"\t"<<buf.st_size<<endl;

  16.         cout<<"time of creation of the file"<<"\t"<<ctime(&buf.st_ctime)<<endl;

  17.         cout<<"time of last modification of the file:"<<"\t"<<ctime(&buf.st_mtime)<<endl;

  18.         cout<<"time of last access of the file:"<<"\t"<<ctime(&buf.st_atime)<<endl;

  19.     }

  20.     getchar();

  21.     return 0;

  22. }

OUTPUT:

size of the file in bytes:      6083826

time of creation of the file    Mon Dec 20 15:44:58 2010

 

time of last modification of the file:  Sun Dec 19 20:37:57 2010

 

time of last access of the file:        Mon Dec 20 15:44:58 2010

文件属性: 

(该文件夹中不存在2.MP3,所以:

  1. #include<sys/stat.h>

  2. #include<iostream>

  3. #include<ctime>

  4.  

  5.  

  6. using namespace std;

  7.  

  8. int main()

  9. {

  10.     struct stat buf;

  11.     int result;

  12.     result=stat("2.mp3",&buf);//stat()函数获取文件状态,成功返回0,失败返回-1。

  13.     if(result!=0)

  14.                  perror("Failed !");

  15.     else

  16.     {

  17.         cout<<"size of the file in bytes:"<<"\t"<<buf.st_size<<endl;

  18.         cout<<"time of creation of the file"<<"\t"<<ctime(&buf.st_ctime)<<endl;

  19.         cout<<"time of last modification of the file:"<<"\t"<<ctime(&buf.st_mtime)<<endl;

  20.         cout<<"time of last access of the file:"<<"\t"<<ctime(&buf.st_atime)<<endl;

  21.     }

  22.     getchar();

  23.     return 0;

  24. }

output:
Failed !: No such file or directory

阅读(786) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~