zoukankan      html  css  js  c++  java
  • C++ 读取指定路径下所有的文件 (C++ get the list of files in a directory)

    电脑配置:window10, 64位操作系统,基于x64的处理器,Microsoft Visual Studio Community 2019 Version 16.4.5

    实现方法:使用 boost-filessystem 包。

    使用 vcpkg 安装方法: .vcpkg.exe install boost-filesystem:x64-windows

    代码:

     1 #include <iostream>
     2 #include <boost/filesystem.hpp>
     3 
     4 using namespace std;
     5 using namespace boost::filesystem;
     6 
     7 int main()
     8 {
     9     //指定图片读取文件夹,然后得到文件夹下的所有图片
    10     string pathImageFile = "e:/picture";
    11     path pathFile(pathImageFile);
    12     vector<string> imageFiles;
    13     for (auto f = directory_iterator(pathFile); f != directory_iterator(); f++)
    14     {
    15         if (!is_directory(f->path()))   // We eliminate directories
    16         {
    17             imageFiles.push_back(f->path().filename().string());
    18             cout << f->path().filename().string() << endl;
    19         }
    20         else
    21             continue;
    22     }
    23 }

    运行结果:

    查阅了一些这方便的中文资料,发现很少谈到要引用包的,所以这里写了下使用第三方包的方法供大家参考。另外,因为本人从 python 转过来,习惯了各式各样的包,使用第三方包往往可以节省我们写代码的时间,所以本人比较喜欢使用第三方软件。查阅了英文的资料,发现可以实现得到指定文件名下文件列表的包除了 boost-filesystem 以外还比较常用有 dirent 这个包,具体的大家可以参考以下链接,以上的代码也是其中的答案之一。

    https://stackoverflow.com/questions/612097/how-can-i-get-the-list-of-files-in-a-directory-using-c-or-c

  • 相关阅读:
    CrackMe17
    逆向按钮事件定位
    CrackMe20
    CrackMe14
    CrackMe09
    CrackMe08
    分布式事务seata
    SpringBoot自动注入原理初解与实现
    InnoDB事务日志(redo log 和 undo log)详解
    高频面试题:Spring 如何解决循环依赖?
  • 原文地址:https://www.cnblogs.com/ttweixiao-IT-program/p/12458319.html
Copyright © 2011-2022 走看看