提高directory_iterator例子 - 如何列出不是递归的目录中的文件 [英] boost directory_iterator example - how to list directory files not recursive

查看:368
本文介绍了提高directory_iterator例子 - 如何列出不是递归的目录中的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我应该如何使用 directory_iterator 列出目录中的文件(不递归)?

还有什么头文件/库,我应该添加/链接或其他设置,我应该做?我在我的项目,但由于某种原因 directory_iterator 使用boost是underclared标识,而我可以用其他升压功能。

UPD 另一种解决方案:

 的#include<文件系统>
     #包括LT&;升压/ filesystem.hpp>
     #包括LT&;&iostream的GT;    使用空间boost ::文件系统;对于(directory_iterator ITR(path_ss); ITR = directory_iterator(!); ++ ITR)
{
    COUT<< itr-方式>路径()文件名()&所述;&下; '';只有//显示文件名
    如果(is_regular_file(itr->状态()))COUT<< [<< FILE_SIZE(itr->路径())≤;&下; ']';
    COUT<<的'\\ n';
}


解决方案

tut3例如 是您要查找的内容:

看它的 住在Coliru

下面是基于C ++ 11的简化版本:

<大骨节病> 住在Coliru

 的#include&LT;升压/ filesystem.hpp&GT;
#包括LT&;升压/范围/ iterator_range.hpp&GT;
#包括LT&;&iostream的GT;使用空间boost ::文件系统;INT主(INT ARGC,CHAR *的argv []){
    路径p(argc个大于1的argv [1]:。);    如果(is_directory(P)){
        性病::法院LT&;&LT; P&LT;&LT; 是包含一个目录:\\ n;        为(自动&安培;条目:升压:: make_iterator_range(directory_iterator(P),{}))
            性病::法院LT&;&LT;进入&LT;&LT; \\ n;
    }
}

您可以看到我挂boost_system(的错误code设施)和boost_filesystem:

  G ++ -std = C ++ 11 -Os -Wall -pedantic的main.cpp -lboost_system -lboost_filesystem&放大器;&安培; ./a.out。
。是包含目录:
./main.cpp
./a.out

How should I use directory_iterator to list directory files (not recursive)?

Also what header files / libs should I add/link or other settings I should make? I'm using boost in my project but by some reason directory_iterator is "underclared identifier" while I can use other boost features.

upd another solution:

     #include <filesystem>
     #include <boost/filesystem.hpp>
     #include <iostream>

    using namespace boost::filesystem;

for (directory_iterator itr(path_ss); itr!=directory_iterator(); ++itr)
{
    cout << itr->path().filename() << ' '; // display filename only
    if (is_regular_file(itr->status())) cout << " [" << file_size(itr->path()) << ']';
    cout << '\n';
}

解决方案

The tut3 example is what you're looking for:

See it Live on Coliru

Here's a simplified version based on c++11:

Live On Coliru

#include <boost/filesystem.hpp>
#include <boost/range/iterator_range.hpp>
#include <iostream>

using namespace boost::filesystem;

int main(int argc, char *argv[]) {
    path p(argc>1? argv[1] : ".");

    if(is_directory(p)) {
        std::cout << p << " is a directory containing:\n";

        for(auto& entry : boost::make_iterator_range(directory_iterator(p), {}))
            std::cout << entry << "\n";
    }
}

You can see I linked boost_system (for the errorcode facilities) and boost_filesystem:

g++ -std=c++11 -Os -Wall -pedantic main.cpp -lboost_system -lboost_filesystem && ./a.out .
"." is a directory containing:
"./main.cpp"
"./a.out"

这篇关于提高directory_iterator例子 - 如何列出不是递归的目录中的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆