[boost]抓取某個目錄下的所有檔案(指定副檔名)

posted in: boost, C/C++程式設計 | 0
#include 
#define BOOST_FILESYSTEM_VERSION 3
#include "vector"

vector getAllFile(string path, string fileType){
	vector files;
	namespace BFS = boost::filesystem;
	BFS::path p1(path);
	std::cout < < "Files in " << p1 << std::endl;
	for( BFS::directory_iterator it = BFS::directory_iterator(p1);it != BFS::directory_iterator(); ++it ){
		BFS::path px = it->path();
		string extension = px.extension();//副檔名
		if(extension == fileType){
			string path = px.string();//轉字串
			files.push_back(path);
		}
	}
	return files;
}

呼叫:
vector filesHtml = getAllFile(".\\D", ".html");//抓D目錄下所有html檔案
vector filesTxt = getAllFile(".\\D", ".txt");//抓D目錄下所有txt檔

參考:http://viml.nchc.org.tw/blog/paper_info.php?CLASS_ID=1&SUB_ID=1&PAPER_ID=195

Leave a Reply

Your email address will not be published. Required fields are marked *