使用C 列出目录中的文件,而不是递归,只有文件和无子目录
List files in a directory, not recursive, only files and no subdirectories, using C++
这是一个后续问题,用于提升目录示例 - 如何列出目录文件不递归。
程序
#include <boost/filesystem.hpp>
#include <boost/range.hpp>
#include <iostream>
using namespace boost::filesystem;
int main(int argc, char *argv[])
{
path const p(argc>1? argv[1] : ".");
auto list = [=] { return boost::make_iterator_range(directory_iterator(p), {}); };
// Save entries of 'list' in the vector of strings 'names'.
std::vector<std::string> names;
for(auto& entry : list())
{
names.push_back(entry.path().string());
}
// Print the entries of the vector of strings 'names'.
for (unsigned int indexNames=0;indexNames<names.size();indexNames++)
{
std::cout<<names[indexNames]<<"n";
}
}
在目录中列出文件,而不是递归,但也列出了子目录的名称。我只想列出文件,而不是子目录。
如何更改代码以实现此目标?
在目录中列出文件,而不是递归,但也列出了 子目录的名称。我只想列出文件而不是 子目录。
您可以使用boost::filesystem::is_directory
过滤目录并仅添加文件:
std::vector<std::string> names;
for(auto& entry : list())
{
if(!is_directory(entry.path()))
names.push_back(entry.path().string());
}
相关文章:
- 递归计数给定目录的文件和所有目录
- 包含模板文件的递归会导致编译失败
- 递归地将FEATURE_FLAG导出到生成文件、CPP 和头文件
- 如何递归复制文件和目录
- 使用FindFirstFile和FindNextFile递归查找具有特定扩展名的文件
- boost::文件系统递归获取每个文件的大小
- C++ 源代码的递归生成文件
- 问题 - 递归函数以返回文本文件排列
- 使用C 列出目录中的文件,而不是递归,只有文件和无子目录
- 列出MFC错误中递归运行的所有文件
- 同一生成文件中的 make 目标之间的递归依赖关系
- 递归递归文件
- 递归扫描目录中的文件
- 使用boost递归地将给定目录中每个最低级别文件夹的文件复制到另一个目录-C++
- 输入所有子文件夹 - 递归
- 用于列出子目录中所有文件的递归功能
- 使用递归获取某个目录中的所有文件
- Qt vs Boost 文件系统递归文件计数
- 如何用C++递归地从二进制文件中读取自定义字符串
- 在Qt中递归浏览目录,跳过文件夹"."并".."