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;
}