您的位置:首页 > 编程语言 > C语言/C++

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++文件遍历