C++读取当前文件夹下所有某种类型文件

参考:http://www.uml.org.cn/c++/201208075.asp


#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main(int argc, char* argv[]){
	//创建文件名列表文件,若存在则清空文件
	fstream file_list("name.txt", std::ios::out);
	file_list.close();

	//写入文件名列表到file_list.txt
	system("dir /a /b >> name.txt");

	file_list.open("name.txt", std::ios::in);
	string filename;
	string extension;

	while(!file_list.eof()){
		getline(file_list, filename);
		int location = filename.find(".", 0);
		if(location != string::npos){
			extension = filename.substr(location);
			if(0 == extension.compare(".cpp")){
				// do something with file 
				cout << filename << endl;
			}
		}
	}

	system("PAUSE");
}


你可能感兴趣的:(C++读取当前文件夹下所有某种类型文件)