主要是参考他的代码,写的不错,很受用!狠狠的复习了一把递归····
https://www.cnblogs.com/fnlingnzb-learner/p/6424563.html
# include<iostream>
# include<io.h>
# include<string>
# include<direct.h>
# include<vector>
using namespace std;
void getFiles(string path, vector<string>& files);
void get_files(string path, vector<string>& files);
int main()
{
//获取当前路径:
char path1[1024];
getcwd(path1, 1024);
string path = path1;
printf("当前路径为:%s\n", path.c_str());
//查找下级目录和当前文件夹文件名称
vector<string> files;
get_files(path, files);
for (auto &ele : files)
{
cout << endl;
printf("%s", ele.c_str());
}
getchar();
return 0;
}
void get_files(string path, vector<string>& files)
{
struct _finddata_t document;
string p;
intptr_t index = 0;
if ((index = _findfirst(p.assign(path).append("\\*").c_str(), &document)) != -1)
{
do
{
if ((document.attrib & _A_SUBDIR))
{
if (strcmp(document.name, ".") != 0 && strcmp(document.name,"..")!=0)
{
get_files(p.assign(path).append("\\").append(document.name), files);
}
}
else
{
if (strcmp(document.name, ".") != 0 && strcmp(document.name, "..") != 0)
{
files.push_back(p.assign(path).append("\\").append(document.name));
}
}
} while (!_findnext(index,&document));
_findclose(index);
}
}
unsigned atrrib:
文件属性的存储位置。它存储一个unsigned单元,用于表示文件的属性。文件属性是用位表示的,主要有以下一些:
_A_ARCH(存档)、 _A_HIDDEN(隐藏)、_A_NORMAL(正常)、
_A_SUBDIR(文件夹)、_A_RDONLY(只读)、_A_SYSTEM(系统)