易采站长站为您分析C++获得文件状态信息的方法,包括文件状态信息、文件所在磁盘盘符、文件创建时间、访问时间及修改日期等,需要的朋友可以参考下
本文实例讲述了C++获得文件状态信息的方法。。具体如下:
- //C++ 获得文件状态信息源码, //C++ 获得文件所在磁盘盘符源码,
- //C++ 文件创建时间源码, //C++ 访问时间源码,
- //C++ 最后修改日期源码,No such file or directory(无此文件或索引) #include<iostream.h>
- #include <time.h> #include <sys/types.h>
- #include <sys/stat.h> #include <stdio.h>
- void main( void ) {
- struct stat buf; int result;
- //获得文件状态信息 result =stat( "D:ok2002.txt", &buf );
- //显示文件状态信息 if( result != 0 )
- perror( "显示文件状态信息出错" );//并提示出错的原因,如No such file or directory(无此文件或索引) else
- { cout<<"文件大小:"<<buf.st_size<<"字节"<<endl;
- cout<<"所在磁盘盘符 :"; cout<<char(buf.st_dev + 'A')<<endl;
- cout<<"文件创建时间:"<<ctime(&buf.st_ctime); cout<<"访问日期:"<<ctime(&buf.st_atime);//注意这里访问时间为00:00:00为正常
- cout<<"最后修改日期:"<<ctime(&buf.st_mtime); }
- } //请读者打开你的D盘,将ok2002.txt修改成你的D盘现有的文件,或新建一个ok2002.txt,再运行以上代码
- //将运行结果与打开你的D盘在ok2002.txt上点右键/选择属性,弹出窗口上的信息比较,信息是否一至
希望本文所述对大家的C++程序设计有所帮助。










