c打开指定路径文件_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

代码:

#include #include 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;    }}

运行结果:

35045d9ae5eff864e4293449aa0f42ef.png

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

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值