文件目录如下,文件夹里还有一些txt文件未展示出来。
使用递归实现,深度优先遍历文件夹中的文件。
代码如下,用了一点C++的语法。
#include <io.h>
#include <iostream>
using namespace std;#define MAX_PATH_LENGTH 100int Traverse(char dir[]);int main()
{char dir[MAX_PATH_LENGTH] = "e:\\test\\*.*";Traverse(dir);return 0;
}int Traverse(char dir[])
{intptr_t handle;_finddata_t findData;handle = _findfirst(dir, &findData);if (handle == -1){cout << "no file exsit\n";return -1;}do{if ((findData.attrib & _A_SUBDIR) && (strcmp(findData.name, ".") != 0) && (strcmp(findData.name, "..") != 0)){//it is a directorycout << "subdir:" << findData.name << endl;char sub_dir[MAX_PATH_LENGTH] = { 0 };string s(dir);sprintf_s(sub_dir, "%s%s\\*.*", s.substr(0, s.length() - 3).c_str(), findData.name);Traverse(sub_dir);}else if (strcmp(findData.name, ".") == 0 || strcmp(findData.name, "..") == 0){//it is . or .. , do nothing}else{//it is a filecout << "file:" << findData.name << endl;}} while (_findnext(handle, &findData) == 0);_findclose(handle);
}
运行结果如下: