基于C++实现读取指定路径文件

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

基于C++实现读取指定路径文件

ttweixiao9999   2020-07-24 我要评论

本文着重讲解了基于C++实现读取指定路径文件,文中通过代码实例讲解的非常细致,对大家的工作和学习具有一定的参考学习价值,欢迎大家阅读和收藏

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

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

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

代码:

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

using namespace std;
using namespace boost::filesystem;

int main()
{
  //指定图片读取文件夹,然后得到文件夹下的所有图片
  string pathImageFile = "e:/picture";
  path pathFile(pathImageFile);
  vector<string> imageFiles;
  for (auto f = directory_iterator(pathFile); f != directory_iterator(); f++)
  {
    if (!is_directory(f->path()))  // We eliminate directories
    {
      imageFiles.push_back(f->path().filename().string());
      cout << f->path().filename().string() << endl;
    }
    else
      continue;
  }
}

运行结果:

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

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

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们