c++17 filesystem, regex 遍歷目錄


linux 遍歷目錄+文件(優化版本)

c++17 FS 還是挺好用的,

VS2017支持,但是linux g++7.3 還是不支持 filesystem


#include<filesystem>
#include<regex>  //正則表達式

namespace fs = std::experimental::filesystem;


int main()
{
    string strPath = "D:\\pic\\new";
    regex fileSuffix("(.*)(.jpg)");// *.jpg, *.png  

    //regex fileSuffix("(.*).(.jpg)"); 也行
    //regex fileSuffix(".*z.*\\.(jpg|png)");//包含字母z的所有jpg或png圖片

    for (auto&DirectoryIter : fs::directory_iterator(strPath))
    {
    	auto filepath = DirectoryIter.path();
    	auto filename = filepath.filename();

	    if (std::regex_match(filename.string(), fileSuffix))
	    {
		vecFilePath.push_back(filepath.string());
		cout << filepath << endl;
	    }

	//replace_extension替換擴展名
	//stem去掉擴展名
    }
}

============

20190813
cpp20 也出來了, g++8.0 貌似也支持std::filesystem了

https://en.cppreference.com/w/cpp/filesystem


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM