C、C++文件夹文件遍历
2016-06-23 14:26
288 查看
C/C++文件夹文件遍历
#include<iostream> #include<string> #include<io.h> #include<time.h> using namespace std; void visit(string path) //文件夹的遍历 { struct _finddata_t filefind; //文件结构体 string str_curr = path + "\\*.*"; struct tm *time; //文件时间 long handle=_findfirst(str_curr.c_str(), &filefind); if(handle == -1) // 判断是否为空文件夹 { return; } while(!(_findnext(handle, &filefind))) { if(strcmp(filefind.name,"..") == 0 || strcmp(filefind.name,".") == 0) { continue; } if((_A_SUBDIR==filefind.attrib)) //是目录 { time = gmtime(&filefind.time_access); cout<<filefind.name << "\t"; cout << time->tm_year << "-" << time->tm_mon << "-" << time->tm_mday; cout << "\t"; cout << time->tm_hour << "-" << time->tm_min << "-" << time->tm_sec << endl; string prepath = path +"\\" + filefind.name; visit(prepath); } else//不是目录,是文件 { cout<<filefind.name<<endl; } } _findclose(handle); } int main() { string path; cout<<"请输入目录"<<endl; cin>>path; visit(path); return 0; } /* struct _finddata_t { unsigned attrib; //文件属性的存储位置 time_t time_create; //文件创建时间 time_t time_access; //文件最后一次被访问的时间 time_t time_write; //文件最后一次被修改的时间。 fsize_t size; //文件的字节数 char name[_MAX_FNAME]; //文件的文件名 }; */
相关文章推荐
- C语言笔记系列(二)--基本数据类型
- C语言的面向对象写法
- c/c++中float和double类型的存储
- C++之友元:友元函数和友元类详解
- STL之list
- C++ STL 之 unordered_set 介绍
- C++读写excel文件(一)—— 用ODBC读写
- C++ 类的对象管理模型初讲
- C++读写excel文件(三)—— 用OLE读写
- C++读写EXCEL文件方式比较
- c++自定义类型
- c++指针函数的使用——回调函数
- Another kind of Fibonacci
- USACO2016 Jan Bronze 4:Promotion Counting
- USACO2016 Jan Bronze 1:Promotion Counting
- USACO2016 Jan Bronze 3:Mowing the Field
- USACO2016 Jan Bronze 2:Angry Cows
- 飛飛(八十二)(补)阅读程序
- 飛飛(八十一)(补)阅读程序
- [leetcode] Add Digits