栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

使用 C++ filesystem 递归目录

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

使用 C++ filesystem 递归目录

如何使用 C++ filesystem 递归目录?
#include 
#include 
#include 
#include 
#include 

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 child) {
		children.push_back(child);
	}
	std::string name() {
		return w2a(path.native());
	}
	std::vector> data() {
		return children;
	}
	std::filesystem::path path;
	std::vector> children;
};

// none member function
void recursion(const std::filesystem::path path, std::shared_ptr& node) {
	for (auto& p : std::filesystem::directory_iterator(path)) {
		if (p.is_directory()) {
			auto newNode = std::make_shared(p.path());
			node->addChild(newNode);
			recursion(p.path(), newNode);
			std::wcout << p.path().native() << 'n';
		}
	}
}

void recursion_node(std::fstream& fs, std::shared_ptr& node, int depth) {
	std::string str = "";
	for (int i = 0; i < depth; i++) {
		str += "t";
	}

	fs << node->name() << std::endl;
	depth++;
	for (auto& v : node->data()) {
		fs << str;
		recursion_node(fs, v, depth);
	}
}

void print(const std::string& filename, std::shared_ptr& root) {
	std::fstream fs(filename, std::fstream::trunc | std::fstream::out);
	if (!fs.is_open()) {
		std::cout << "failed to open " << filename << std::endl;
		return;
	}

	recursion_node(fs, root, 1);
}


int main()
{
	auto rc = std::filesystem::path(R"(A:Temproot)");

	auto root = std::make_shared(rc);
	recursion(rc, root);

	print("aaaa4.txt", root);
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1035854.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号