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

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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值