如何使用 C++ filesystem 递归目录?
#include <Windows.h>
#include <iostream>
#include <string>
#include <fstream>
#include <filesystem>
std::string w2a(const std::wstring& utf8)
{
int buffSize = WideCharToMultiByte(CP_UTF8, NULL, utf8.c_str(), -1, NULL, NULL, NULL, FALSE);
char* bf = new char[buffSize + 1];
memset(bf, 0, buffSize + 1);
WideCharToMultiByte(CP_UTF8, NULL, utf8.c_str(), -1, bf, buffSize, NULL, FALSE);
std::string result(bf);
delete[] bf;
bf = nullptr;
return result;
}
struct TreeNode
{
TreeNode(const std::filesystem::path newPath) {
path = newPath;
}
void addChild(std::shared_ptr<TreeNode> child) {
children.push_back(child);
}
std::string name() {
return w2a(path.native());
}
std::vector<std::shared_ptr<TreeNode>> data() {
return children;
}
std::filesystem::path path;
std::vector&