C++ 使用C+列出目录中的文件,而不是递归文件,只列出文件而不列出子目录+;
这是我们的后续问题 节目C++ 使用C+列出目录中的文件,而不是递归文件,只列出文件而不列出子目录+;,c++,boost,C++,Boost,这是我们的后续问题 节目 #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 = [=] { r
#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;
int main(int argc,char*argv[])
{
路径常数p(argc>1?argv[1]:”);
自动列表=[=]{return boost::make_iterator_range(目录_iterator(p),{});};
//在字符串“名称”向量中保存“列表”项。
std::矢量名称;
用于(自动输入:列表())
{
name.push_back(entry.path().string());
}
//打印字符串“名称”向量的条目。
对于(无符号int indexNames=0;indexNames)
列出目录中的文件,不是递归的,但也列出
子目录的名称。我只想列出文件,不想列出
子目录
您可以使用boost::filesystem::is_directory
筛选出目录并仅添加文件:
std::vector<std::string> names;
for(auto& entry : list())
{
if(!is_directory(entry.path()))
names.push_back(entry.path().string());
}
std::向量名称;
用于(自动输入:列表())
{
如果(!is_目录(entry.path()))
name.push_back(entry.path().string());
}
列出目录中的文件,不是递归的,但也列出
子目录的名称。我只想列出文件,不想列出
子目录
您可以使用boost::filesystem::is_directory
筛选出目录并仅添加文件:
std::vector<std::string> names;
for(auto& entry : list())
{
if(!is_directory(entry.path()))
names.push_back(entry.path().string());
}
std::向量名称;
用于(自动输入:列表())
{
如果(!is_目录(entry.path()))
name.push_back(entry.path().string());
}
它是否接受一个目录\u条目
作为参数?根据,它将接受一个文件\u状态
或一个路径
是常规的\u文件
将跳过其他内容(例如链接)。有一个是\u目录
函数可能更合适。也许是\u目录(entry.path())
?@BenjaminLindley,我的错。Corrected@BorisGlick,你是对的。is\u directory
更适合于此。谢谢它是否接受目录项作为参数?根据,它将接受文件状态
或路径
is\u常规文件
将跳过其他内容(例如,链接)。有一个is_directory
函数可能更合适。也许is_directory(entry.path())
?@BenjaminLindley,我错了。Corrected@BorisGlick,你说得对。是目录
更适合这个。谢谢