使用boost库获取递归文件名



我尝试使用以下函数递归地获取文件名。但我只有一个子文件夹名称(如:/media/username)。为了理解这个问题,我将路径值写入/media/username而不是/media,但这次我无法获得任何文件名。/media目录有特殊的规则吗?如果是,我该如何克服?如果没有,为什么我不能得到文件名?

获取函数如下:

void obtain_filenames()
{
for(const auto& p : fs::recursive_directory_iterator("/media"))
m_filenames.push_back(p.path().string());
}

没有特别的规则。你要求文件名,所以你得到了它们。如果你想要完整路径,你可以这样做:

for(const auto& p : fs::recursive_directory_iterator(directory))
paths.push_back(fs::absolute(p.path()));

注意,这会给你整个路径(所以如果你使用".",你可能会得到"/home/purgoufr/.bashrc",而不是"/bashrc")。如果你不想这样,看看' make_relative",例如:

paths.push_back(directory / fs::relative(p.path(), directory));

指出:

  • 请记住权限错误可能会破坏迭代器循环(参见fs::directory_options::pop_on_error)
  • 盲目地递归迭代任何目录可能导致(安全)问题。在这种情况下,您可能会耗尽内存,以无限循环结束(参见fs::directory_options::follow_directory_symlink)等。

演示显示所提到的一些改进和更多:

Live On Coliru

#include <boost/filesystem.hpp>
namespace fs = boost::filesystem;
using Paths = std::vector<fs::path>;
Paths obtain_filenames(fs::path base, size_t max)
{
Paths paths;
for(const auto& p : fs::recursive_directory_iterator(base, fs::directory_options::pop_on_error))
{
// paths.push_back(fs::absolute(p.path().string()));
paths.push_back(base / fs::relative(p.path(), base));
if (paths.size() >= max)
break;
}
return paths;
}
#include <iostream>
int main(int argc, char** argv) {
for (auto arg : std::vector(argv + 1, argv + argc)) {
for (auto const& path : obtain_filenames(arg, 5)) {
std::cout << path << "n";
}
}
}

。当运行与'。/说. ./stackoverflow/*/'在我的系统上打印

"../stackoverflow/asio/CMakeLists.txt"
"../stackoverflow/asio/Service.h"
"../stackoverflow/asio/Acceptor.cpp"
"../stackoverflow/asio/main.cpp"
"../stackoverflow/asio/Service.cpp"
"../stackoverflow/boost-dll-example/.devcontainer"
"../stackoverflow/boost-dll-example/.devcontainer/devcontainer.json"
"../stackoverflow/boost-dll-example/.devcontainer/Dockerfile"
"../stackoverflow/boost-dll-example/.git"
"../stackoverflow/boost-dll-example/.git/info"
"../stackoverflow/bucket/5f7d8fe7-be61-4182-9553-8aa4dceefd36"
"../stackoverflow/bucket/5f7d8fe7-be61-4182-9553-8aa4dceefd36/q.png"
"../stackoverflow/bucket/59caf141-803d-41dc-b7b0-fd6601e88295"
"../stackoverflow/bucket/0605ef7a-0bcf-4b00-a3fe-ef2560758008"
"../stackoverflow/bucket/718b317d-bc98-4012-a2c9-d1453edf7771"
"../stackoverflow/CMakeFiles/CMakeRuleHashes.txt"
"../stackoverflow/CMakeFiles/sotest.dir"
"../stackoverflow/CMakeFiles/sotest.dir/CXX.includecache"
"../stackoverflow/CMakeFiles/sotest.dir/depend.make"
"../stackoverflow/CMakeFiles/sotest.dir/DependInfo.cmake"
"../stackoverflow/cppcoro/args.cake"
"../stackoverflow/cppcoro/.clang-format"
"../stackoverflow/cppcoro/config.cake"
"../stackoverflow/cppcoro/.git"
"../stackoverflow/cppcoro/.git/hooks"
"../stackoverflow/cpp-sort/build"
"../stackoverflow/cpp-sort/build/cmake_install.cmake"
"../stackoverflow/cpp-sort/build/compile_commands.json"
"../stackoverflow/cpp-sort/build/Catch2-build"
"../stackoverflow/cpp-sort/build/Catch2-build/CTestTestfile.cmake"
"../stackoverflow/detail/yield.hpp"
"../stackoverflow/eos-portable-archive/readme"
"../stackoverflow/eos-portable-archive/change_log.txt"
"../stackoverflow/eos-portable-archive/tutorial"
"../stackoverflow/eos-portable-archive/tutorial/code"
"../stackoverflow/eos-portable-archive/tutorial/code/tutorial_pba_10b.cpp"
"../stackoverflow/eventpp/.gitignore"
"../stackoverflow/eventpp/license"
"../stackoverflow/eventpp/.github"
"../stackoverflow/eventpp/.github/workflows"
"../stackoverflow/eventpp/.github/workflows/main.yml"
"../stackoverflow/fmt/cmake_install.cmake"
"../stackoverflow/fmt/support"
"../stackoverflow/fmt/support/rtd"
"../stackoverflow/fmt/support/rtd/theme"
"../stackoverflow/fmt/support/rtd/theme/theme.conf"
"../stackoverflow/msghub/build"
"../stackoverflow/msghub/build/compile_commands.json"
"../stackoverflow/msghub/build/examples"
"../stackoverflow/msghub/build/examples/cmake_install.cmake"
"../stackoverflow/msghub/build/examples/server"
"../stackoverflow/mymsg/.git"
"../stackoverflow/mymsg/.git/hooks"
"../stackoverflow/mymsg/.git/hooks/applypatch-msg.sample"
"../stackoverflow/mymsg/.git/hooks/pre-rebase.sample"
"../stackoverflow/mymsg/.git/hooks/pre-applypatch.sample"
"../stackoverflow/mysql/.gitignore"
"../stackoverflow/mysql/Jamfile"
"../stackoverflow/mysql/tools"
"../stackoverflow/mysql/tools/user_project_find_package"
"../stackoverflow/mysql/tools/user_project_find_package/CMakeLists.txt"
"../stackoverflow/networking-ts-impl/.git"
"../stackoverflow/networking-ts-impl/.git/logs"
"../stackoverflow/networking-ts-impl/.git/logs/HEAD"
"../stackoverflow/networking-ts-impl/.git/logs/refs"
"../stackoverflow/networking-ts-impl/.git/logs/refs/remotes"
"../stackoverflow/nghttp2/Makefile.am"
"../stackoverflow/nghttp2/makemanpages"
"../stackoverflow/nghttp2/help2rst.py"
"../stackoverflow/nghttp2/fedora"
"../stackoverflow/nghttp2/fedora/spdylay.spec"
"../stackoverflow/obj/testera.o"
"../stackoverflow/obj/testerb.o"
"../stackoverflow/pfr/meta"
"../stackoverflow/pfr/meta/libraries.json"
"../stackoverflow/pfr/test"
"../stackoverflow/pfr/test/compile-fail"
"../stackoverflow/pfr/test/compile-fail/non_aggregate.cpp"
... and so on

相关内容

最新更新