C ++ - 加载所有文件名+计数当前目录中的文件数+过滤文件扩展名 [英] C++ - Load all filename + count the number of files in a current directory + filter file extension

查看:165
本文介绍了C ++ - 加载所有文件名+计数当前目录中的文件数+过滤文件扩展名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想计算当前目录中的文件数,以及加载数组中的所有文件名。如果可能,我想知道如何过滤文件扩展名

I want to count the number of file in the current directory as well as load all file names in the array. If possible, I want to know how to filter file extension also

推荐答案

lboost_filesystem

#include <iostream>
#include <string>
#include <vector>

#include <boost/algorithm/string/case_conv.hpp>
#include <boost/filesystem.hpp>

int main( int argc, char ** argv )
{
  std::string ext = ".jpg";

  std::vector<std::string> files;

  for ( boost::filesystem::directory_iterator it( boost::filesystem::initial_path() );
        it != boost::filesystem::directory_iterator(); ++it )
  {
    if ( boost::filesystem::is_regular_file( it->status() ) &&
         boost::algorithm::to_lower_copy( it->path().extension() ) == ext )
    {
      files.push_back( it->path().filename() );
    }
  }

  std::cout << "Number of files: " << files.size() << std::endl;
  std::copy( files.begin(), files.end(), std::ostream_iterator<std::string>( std::cout, "\n" ) );

  return 0;
}

这篇关于C ++ - 加载所有文件名+计数当前目录中的文件数+过滤文件扩展名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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