I have the following directory tree :
test_dir
a
f3.txt
b
f1.txt
f2.txt
I want to traverse this folder and print it as follows :
test_dir
b
f2.txt
f1.txt
a
f3.txt
For this I developed the following code snippet :
struct EntryInfo {
std::filesystem::path path_;
std::filesystem::file_type type_;
std::uint32_t level_;
};
void PrintTree(ostream& dst,
const path& p,
const filesystem::file_status& status,
std::vector<EntryInfo>& info,
std::uint32_t level)
{
if (status.type() == std::filesystem::file_type::directory) {
info.push_back({p, std::filesystem::file_type::directory, level});
for (const auto & entry : std::filesystem::directory_iterator(p)) {
PrintTree(dst, entry.path(), entry.status(), info, level + 1);
}
} else {
info.push_back({p, std::filesystem::file_type::regular, level});
}
}
void PrintTree(ostream& dst, const path& p)
{
std::vector<EntryInfo> info;
for (const auto & entry : std::filesystem::directory_iterator(p)) {
PrintTree(dst, entry.path(), entry.status(), info, 1);
}
std::sort(info.begin(), info.end(), [](const EntryInfo& lhs, const EntryInfo& rhs) {
// smth here?
return true;
});
std::cout << p.string() << std::endl;
for (const auto& item : info) {
std::cout << std::string(item.level_*2, ' ') << item.path_.filename().string() << std::endl;
}
Is it possible to sort this vector to get the desired order of items to be printed? Or I'm wrong trying to store entries inside a vector? What is the right thing to do here? Any ideas are highly appreciated
Here is a small test case I expect to pass :
#include <cassert>
#include <filesystem>
#include <fstream>
#include <iostream>
#include <sstream>
#include <string>
#include <string_view>
#include <vector>
#include <algorithm>
using namespace std;
using filesystem::path;
path operator""_p(const char* data, std::size_t sz) {
return path(data, data + sz);
}
int main() {
error_code err;
filesystem::remove_all("test_dir", err);
filesystem::create_directories("test_dir"_p / "a"_p, err);
filesystem::create_directories("test_dir"_p / "b"_p, err);
ofstream("test_dir"_p / "b"_p / "f1.txt"_p);
ofstream("test_dir"_p / "b"_p / "f2.txt"_p);
ofstream("test_dir"_p / "a"_p / "f3.txt"_p);
ostringstream out;
PrintTree(out, "test_dir"_p);
assert(out.str() ==
"test_dir\n"
" b\n"
" f2.txt\n"
" f1.txt\n"
" a\n"
" f3.txt\n"sv
);
}