gpt-3.5-turbo-0301· openai
Suzhen/CodeChat
Windows下C++遍历文件夹下所有文件,然后使用b tree存储结果
- User
Windows下C++遍历文件夹下所有文件,然后使用b tree存储结果
- Assistant
以下是一个Windows下使用C++遍历文件夹并使用b树存储结果的示例代码:
#include <iostream> #include <string> #include <vector> #include <Windows.h> #include "btree/btree_map.h" using namespace std; // 遍历文件夹 void traverseFolder(const string& folderPath, btree::btree_map<string, string>& files) { // 构建查询条件 string search_path = folderPath + "/*.*"; WIN32_FIND_DATAA fd; HANDLE hFind = ::FindFirstFileA(search_path.c_str(), &fd); if (hFind != INVALID_HANDLE_VALUE) { do { // 判断是否为文件夹 if (!(fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) { // 将文件路径存储到 b 树中 files.insert({fd.cFileName, folderPath}); } } while (::FindNextFileA(hFind, &fd)); ::FindClose(hFind); } } int main() { // 定义文件夹路径 string folderPath = "C:\\Users\\username\\Desktop\\test"; // 定义 b 树 btree::btree_map<string, string> files; // 遍历文件夹并存储结果到 b 树中 traverseFolder(folderPath, files); // 遍历 b 树输出结果 for (const auto& file : files) { cout << file.first << " " << file.second << endl; } return 0; }在上面的示例中,我们使用了 btree_map 数据结构来存储文件路径。btree_map 在添加和查找元素时具有很高的性能优势,比 std::map 和 std::unordered_map 更快。遍历文件夹可以使用 Windows API 中的 FindFirstFile 和 FindNextFile 来实现。
如果需要遍历子文件夹,可以使用递归方式遍历文件夹并存储结果到 b 树中。
- User
分割目录路径,节点子存放文件或文件夹名
- Assistant
以下是一个分割目录路径,节点子存放文件或文件夹名的示例代码:
#include <iostream> #include <string> #include <vector> #include <Windows.h> #include "btree/btree_map.h" using namespace std; // 将目录路径分割成路径和文件/文件夹名 void splitPath(const string& path, string& parentPath, string& name) { size_t pos = path.find_last_of("/\\"); if (pos == string::npos) { parentPath = "."; name = path; } else { parentPath = path.substr(0, pos); name = path.substr(pos + 1); } } // 遍历文件夹 void traverseFolder(const string& folderPath, btree::btree_map<string, vector<string>>& nodes) { // 构建查询条件 string search_path = folderPath + "/*.*"; WIN32_FIND_DATAA fd; HANDLE hFind = ::FindFirstFileA(search_path.c_str(), &fd); if (hFind != INVALID_HANDLE_VALUE) { do { string name = fd.cFileName; if (name != "." && name != "..") { string path = folderPath + "/" + name; string parentPath, nodeName; splitPath(path, parentPath, nodeName); auto iter = nodes.emplace(parentPath, vector<string>{}).first; iter->second.push_back(nodeName); } } while (::FindNextFileA(hFind, &fd)); ::FindClose(hFind); } } int main() { // 定义文件夹路径 string folderPath = "C:\\Users\\username\\Desktop\\test"; // 定义 b 树 btree::btree_map<string, vector<string>> nodes; // 遍历文件夹并存储结果到 b 树中 traverseFolder(folderPath, nodes); // 遍历 b 树输出结果 for (const auto& node : nodes) { cout << "Parent Path: " << node.first << endl; cout << "Node Names: "; for (const auto& name : node.second) { cout << name << " "; } cout << endl; } return 0; }在上面的示例中,我们首先定义了一个 splitPath 函数来将目录路径分割成父路径和文件/文件夹名。接下来在 traverseFolder 函数中,将文件/文件夹名存储到对应的父路径节点中。最后遍历 b 树并输出节点信息。
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.