c语言 文件夹遍历 深度,C++ - 深度搜索遍历文件夹

深度搜索遍历文件夹

深度优先搜索遍历文件夹所有文件, 由于使用windows的函数, 必须要使用C语言;

注意字符集的问题,使用"#undef UNICODE", 屏蔽因字符集所产生的问题;

使用vector存储所有文件名, 因为要递归使用, 所以需要设置为静态,返回shared_ptr的指针

代码如下: /************************************************* File: main.cpp Copyright: C.L.Wang Author: Caroline Date: 2013-11-27 Description: 深度优先递归遍历目录中所有的文件 Email: morndragon@126.com **************************************************/ #undef UNICODE #include #include #include #include #include #include std::shared_ptr<:vector> > fileList(const std::string& folder_path) { static std::shared_ptr<:vector> > folder_files(new std::vector<:string>); //返回指针, 需要迭代使用 WIN32_FIND_DATA FindData; HANDLE hError; int file_count(0); std::string file_path(folder_path); //路径名 std::string full_file_path; //全路径名 file_path.append("/*.*"); hError = FindFirstFile(file_path.c_str(), &FindData); if (hError == INVALID_HANDLE_VALUE) { std::cout << "failed to search files." << std::endl; return nullptr; } while(FindNextFile(hError, &FindData)) { //过虑".", "..", "-q" if(0 == strcmp(FindData.cFileName, ".") || 0 == strcmp(FindData.cFileName, "..") || 0 == strcmp(FindData.cFileName, "-q")) { continue; } //完整路径 full_file_path.append(folder_path); full_file_path.append("/"); full_file_path.append(FindData.cFileName); ++file_count; if (FindData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY){ //std::cout << file_count << " " << full_file_path << "

8b459b98c9cdf4993418df892537074d.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值