C++获取指定目录文件列表

#include<iostream>

#include<string>

#include<vector>

#include<fstream>

#include<io.h>

#include<direct.h>

/******************************************************************************************

Function: GetFiles

Description: 获取文件列表

Input: folder_path:文件夹路径

Output: files:用于保存文件路径的数组

*******************************************************************************************/

void GetFiles(std::string folder_path, std::vector& files)

{

//文件句柄

//intptr_t hFile = 0;//Win10

long hFile = 0;

//文件信息 

struct _finddata_t fileinfo;

std::string p;

try

{

if ((hFile = _findfirst(p.assign(folder_path).append("\\*").c_str(), &fileinfo)) != -1)

{

do

{

//如果是目录,迭代之 

//如果不是,加入列表 

if ((fileinfo.attrib &  _A_SUBDIR))

{

if (strcmp(fileinfo.name, ".") != 0 && strcmp(fileinfo.name, "..") != 0)

GetFiles(p.assign(folder_path).append("\\").append(fileinfo.name), files);

}

else

{

files.push_back(p.assign(folder_path).append("\\").append(fileinfo.name));

}

} while (_findnext(hFile, &fileinfo) == 0);

_findclose(hFile);

}

}

catch (std::exception e)

{

}

}

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • mean to add the formatted="false" attribute?.[ 46% 47325/...
    ProZoom阅读 2,719评论 0 3
  • 说在前面 导师要求将人脸图片缩放成尺寸大小相同,刚开始以为网上应该有相应的代码实现,找着找着发现有个叫“Im...
    梦依雪原阅读 926评论 0 1
  • pyspark.sql模块 模块上下文 Spark SQL和DataFrames的重要类: pyspark.sql...
    mpro阅读 9,503评论 0 13
  • •王越:VMware存储API整理,比如核心的存储池与卷两者的CURD。可以参考:VMware in OpenSt...
    笨手笨脚越阅读 5,697评论 0 2
  • 【一】 良言,人如其名。一双薄唇,能抵百万大军,一副皓齿,能挡千万兵器。和平年代,他的家里可谓高朋满座,谈笑有鸿儒...
    瞎掰少女阅读 432评论 0 0