如何使用 boost::fs 只加载 30 个最新文件而不是整个目录?

las*_*lot 4 c++ filesystems directory macos boost

使用 boost 的新手。使用它来加载图像集合。问题是文件夹中的图像数量将继续增长,我最终不想将它们全部添加到我的显示程序中。我在 OS X 上使用 C++。

如何调整此示例代码以仅加载目录顶部或底部的 30 张图像?只加载最新的文件会很棒,但我会满足于改变这一点。不幸的是,在我的循环中只是说 (it <30) 不起作用,因为它需要等效于 fs::directory_iterator。

示例代码:

fs::path pPhoto( photobooth_texture_path );
for ( fs::directory_iterator it( pPhoto ); it != fs::directory_iterator(); ++it )
{
    if ( fs::is_regular_file( *it ) )
    {
        // -- Perhaps there is a better way to ignore hidden files
        string photoFileName = it->path().filename().string();
        if( !( photoFileName.compare( ".DS_Store" ) == 0 ) )
        {
            photoboothTex.push_back( gl::Texture( loadImage( photobooth_texture_path + photoFileName ), mipFmt) );
            cout << "Loaded: " << photoFileName <<endl;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

编辑:这就是我最终这样做的方式。有点是这两种方法的混合体,但我需要倒退排序,即使它不一定是可预见的倒退......抓住机会。不是世界上最干净的东西,但我必须将他们的想法转化为我理解的 C++ 风格

    vector<string> fileList;
int count = 0;

photoboothTex.clear();//clear this out to make way for new photos

fs::path pPhoto( photobooth_texture_path );
for ( fs::directory_iterator it( pPhoto ); it != fs::directory_iterator(); ++it )    {
    if ( fs::is_regular_file( *it ) )
    {
        // -- Perhaps there is a better way to ignore hidden files
        string photoFileName = it->path().filename().string();

        if( !( photoFileName.compare( ".DS_Store" ) == 0 ) )
        {
            fileList.push_back(photoFileName);
        }
    }
}
for (int i=(fileList.size()-1); i!=0; i--) {

        photoboothTex.push_back( gl::Texture( loadImage( photobooth_texture_path + fileList[i%fileList.size()] )) );
        cout << "Loaded Photobooth: " << fileList[i%fileList.size()] <<endl;
        if(++count ==40) break; //loads a maximum of 40 images
}
Run Code Online (Sandbox Code Playgroud)

jro*_*rok 5

这是一个工作示例,它使用 boost::filter_iteratorwithdirectory_iterator将常规文件的路径存储在向量中。我根据 对向量进行排序last_write_time()。为了简洁起见,我还省略了错误检查 - 如果目录中的文件少于 30 个,则此示例将崩溃。

#include <iostream>
#include <vector>
#include <algorithm>
#include <iterator>
#include <boost/filesystem.hpp>
#include <boost/iterator/filter_iterator.hpp>
namespace fs = boost::filesystem;

int main()
{
    fs::path p("My image directory");
    fs::directory_iterator dir_first(p), dir_last;
    std::vector<fs::path> files;

    auto pred = [](const fs::directory_entry& p)
    {
        return fs::is_regular_file(p);
    };

    std::copy(boost::make_filter_iterator(pred, dir_first, dir_last),
              boost::make_filter_iterator(pred, dir_last, dir_last),
              std::back_inserter(files)
              );

    std::sort(files.begin(), files.end(),
              [](const fs::path& p1, const fs::path& p2)
              {
                  return fs::last_write_time(p1) < fs::last_write_time(p2);
              });

    std::copy_n(files.begin(), 30, std::ostream_iterator<fs::path>(std::cout, "\n"));
}
Run Code Online (Sandbox Code Playgroud)

为了使您的示例工作,您可以像这样构建 for 循环:

fs::path pPhoto( photobooth_texture_path );
fs::directory_iterator it( pPhoto );

for ( size_t i = 0;  i < 30 && it != fs::directory_iterator(); ++it )
{
    if ( fs::is_regular_file( *it ) )
    {
        // load the image
        ++i;
    }
}
Run Code Online (Sandbox Code Playgroud)