C、C++文件夹文件遍历

来源:互联网 发布:软件模块起英文名 编辑:程序博客网 时间:2024/05/14 22:13

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];     //文件的文件名};*/
0 0
原创粉丝点击