電腦配置:window10, 64位操作系統(tǒng),基于x64的處理器,Microsoft Visual Studio Community 2019 Version 16.4.5
實現(xiàn)方法:使用 boost-filessystem 包。
使用 vcpkg 安裝方法: .\vcpkg.exe install boost-filesystem:x64-windows
代碼:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
#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 ; } } |
運行結(jié)果:
查閱了一些這方便的中文資料,發(fā)現(xiàn)很少談到要引用包的,所以這里寫了下使用第三方包的方法供大家參考。另外,因為本人從 python 轉(zhuǎn)過來,習(xí)慣了各式各樣的包,使用第三方包往往可以節(jié)省我們寫代碼的時間,所以本人比較喜歡使用第三方軟件。查閱了英文的資料,發(fā)現(xiàn)可以實現(xiàn)得到指定文件名下文件列表的包除了 boost-filesystem 以外還比較常用有 dirent 這個包,具體的大家可以參考以下鏈接,以上的代碼也是其中的答案之一。
https://stackoverflow.com/questions/612097/how-can-i-get-the-list-of-files-in-a-directory-using-c-or-c
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。
原文鏈接:https://www.cnblogs.com/ttweixiao-IT-program/p/12458319.html