参考: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");
}