因为最有有一个需求,需要遍历数据文件下的特定类型文件,发现网上的资料多数都是单一平台,就自己整合了一下。
#ifdef _WIN32
#include <io.h>
#include <string>
#else
#include <unistd.h>
#include <stdio.h>
#include <dirent.h>
#include <sys/stat.h>
#include <string.h>
#endif
#include <iostream>
using namespace std;
void dfsFolder(string folderPath, int depth)
{
#ifdef WIN32
_finddata_t FileInfo;
string strfind = folderPath + "\\*";
long Handle = _findfirst(strfind.c_str(), &FileInfo);
if (Handle == -1L)
{
cerr << "can not match the folder path" << endl;
exit(-1);
}
do{
//判断是否有子目录
if (FileInfo.attrib & _A_SUBDIR)
{
//这个语句很重要
if ((depth - 1 > 0) && (strcmp(FileInfo.name,".") != 0 ) &&(strcmp(FileInfo.name,"..") != 0))
{
string newPath = folderPath + &#