C++11 “如何编写UNIX文件系统”;ls-R/";使用现代C++;11/C++;14/C+;1Z? 学习/理解现代C++的各种概念,我试图编写类似的程序“LS-R/”,递归地列出子目录。为了实现这一点,我使用了强的未来C++文件系统库< /St>,以便程序可以移植。到目前为止,我能够编写以下程序来实现这一点 #include<filesystem> //Other herader files // Below typedef is for VS2013 using fspath = std::tr2::sys::path; using dir_iterator = std::tr2::sys::directory_iterator; using namespace std::tr2::sys; struct directory { std::vector<fspath> files; std::vector<fspath> operator()(const fspath& input) { std::cout << "Input Directory Name: " << input.string() << std::endl; dir_iterator bgnitr(input); dir_iterator enditr; for (dir_iterator itr = bgnitr; itr != enditr; ++itr) { // Only store the directory from input directory, // otherwise display the name fspath tmp = *itr; if (is_directory(tmp)) { files.push_back(tmp); } else { tmp = tmp.filename(); std::cout << tmp.string() << std::endl; } } return files; } }; int main(int argc, const char** argv) { fspath input{argv[1]}; directory dir; auto files = dir(input); std::sort(std::begin(files), std::end(files)); std::for_each(std::begin(files), std::end(files), directory()); return 0; } #包括 //其他herader文件 //以下类型定义适用于VS2013 使用fspath=std::tr2::sys::path; 使用dir\u迭代器=std::tr2::sys::directory\u迭代器; 使用名称空间std::tr2::sys; 结构目录{ std::矢量文件; std::vector操作符()(常量fspath和输入){ std::cout

C++11 “如何编写UNIX文件系统”;ls-R/";使用现代C++;11/C++;14/C+;1Z? 学习/理解现代C++的各种概念,我试图编写类似的程序“LS-R/”,递归地列出子目录。为了实现这一点,我使用了强的未来C++文件系统库< /St>,以便程序可以移植。到目前为止,我能够编写以下程序来实现这一点 #include<filesystem> //Other herader files // Below typedef is for VS2013 using fspath = std::tr2::sys::path; using dir_iterator = std::tr2::sys::directory_iterator; using namespace std::tr2::sys; struct directory { std::vector<fspath> files; std::vector<fspath> operator()(const fspath& input) { std::cout << "Input Directory Name: " << input.string() << std::endl; dir_iterator bgnitr(input); dir_iterator enditr; for (dir_iterator itr = bgnitr; itr != enditr; ++itr) { // Only store the directory from input directory, // otherwise display the name fspath tmp = *itr; if (is_directory(tmp)) { files.push_back(tmp); } else { tmp = tmp.filename(); std::cout << tmp.string() << std::endl; } } return files; } }; int main(int argc, const char** argv) { fspath input{argv[1]}; directory dir; auto files = dir(input); std::sort(std::begin(files), std::end(files)); std::for_each(std::begin(files), std::end(files), directory()); return 0; } #包括 //其他herader文件 //以下类型定义适用于VS2013 使用fspath=std::tr2::sys::path; 使用dir\u迭代器=std::tr2::sys::directory\u迭代器; 使用名称空间std::tr2::sys; 结构目录{ std::矢量文件; std::vector操作符()(常量fspath和输入){ std::cout,c++11,recursion,composite,boost-filesystem,c++17,C++11,Recursion,Composite,Boost Filesystem,C++17,我将重命名您的目录类,它不模拟目录,它是一个打印目录内容的函数 您可以将基于范围的for循环与目录迭代器一起使用,以简化语法: for (auto f : fs::directory_iterator{dir}) 您的程序假定只使用一个引用目录的参数调用它,而ls-R可以使用零个或多个文件或目录参数调用 我会这样做,虽然这可能会得到改进,以简化main中的逻辑,并将其合并到ls函数中: #include <utility> #include <vector> #incl

我将重命名您的
目录
类,它不模拟目录,它是一个打印目录内容的函数

您可以将基于范围的
for
循环与
目录迭代器一起使用,以简化语法:

for (auto f : fs::directory_iterator{dir})
您的程序假定只使用一个引用目录的参数调用它,而
ls-R
可以使用零个或多个文件或目录参数调用

我会这样做,虽然这可能会得到改进,以简化
main
中的逻辑,并将其合并到
ls
函数中:

#include <utility>
#include <vector>
#include <string>
#include <algorithm>
#include <iostream>
#include <experimental/filesystem>

namespace fs = std::experimental::filesystem;

void ls(std::ostream& out, const fs::path& dir)
{
  std::vector<std::pair<std::string, bool>> files;
  for (auto f : fs::directory_iterator{dir})
    files.emplace_back(f.path().filename(), is_directory(f));
  std::sort(files.begin(), files.end());

  // print directory contents
  out << dir.string() << ":\n";
  for (auto& f : files)
    out << f.first << '\n';
  out << std::endl;

  // recurse into directories
  for (auto& f : files)
    if (f.second)
      ls(out, dir / f.first);
}

int main(int argc, char** argv)
{
  if (argc < 2)
    ls(std::cout, ".");
  else
  {
    std::vector<std::string> files;
    std::vector<std::string> dirs;
    for (int i = 1; i < argc; ++i)
      if (fs::is_directory(argv[i]))
        dirs.push_back(argv[i]);
      else
        files.push_back(argv[i]);
    std::sort(files.begin(), files.end());
    for (auto& f : files)
      std::cout << f << '\n';
    std::cout << '\n';
    std::sort(dirs.begin(), dirs.end());
    for (auto& d : dirs)
      ls(std::cout, d);
  }
}
#包括
#包括
#包括
#包括
#包括
#包括
命名空间fs=std::实验::文件系统;
void ls(std::ostream&out,const fs::path&dir)
{
std::矢量文件;
for(自动f:fs::目录迭代器{dir})
files.emplace_back(f.path().filename(),is_directory(f));
排序(files.begin(),files.end());
//打印目录内容

没有TR2。TRs在一段时间前就被废弃了。现在我们有了技术规范,其中很多,每个都集中在一个特定的主题上。您正在寻找(目前正在投票)。这与任何其他递归文件搜索实现有何不同?我很难理解这个问题。您是否要求递归文件列表达到某个深度,如unix的find命令的
-maxdepth
选项?如果第二个int参数指示所需的搜索深度ICH将被递归调用。@ KeRekSB:是的,我使用的是您提到的文件系统TS。@ PTER:我想用现代C++实现“LS -R输入目录”,就像实用工具。这只是为了理解/应用C++和设计模式的各种概念,而不是替换任何UNIX实用工具。我希望你们有我的意图。