您的位置:首页 > 编程语言 > C语言/C++

C++ 获取目录下的文件

2010-03-01 21:19 387 查看
#include <direct.h>
#include <string>
#include <vector>
#include <iomanip>
#include <ctime>
using namespace std;

void getFiles(string, vector<string>&);

int _tmain(int argc, _TCHAR* argv[])
{
vector<string> files;
getFiles(".", files);

// print the files get
for(int j=0; j<files.size(); j++){
cout << files[j] << endl;
}

return 0;
}

void getFiles(string path, vector<string>& files ){
//文件句柄
long hFile = 0;
//文件信息
struct _finddata_t fileinfo;

string p;

if ((hFile = _findfirst(p.assign(path).append("/*").c_str(),&fileinfo)) != -1) {

do {
//如果是目录,迭代之
//如果不是,加入列表
if ((fileinfo.attrib & _A_SUBDIR)){
if (strcmp(fileinfo.name,".") != 0 && strcmp(fileinfo.name,"..") != 0)
getFiles( p.assign(path).append("/").append(fileinfo.name), files );
} else {
files.push_back( p.assign(path).append("/").append(fileinfo.name) );
}
} while (_findnext( hFile, &fileinfo ) == 0);

_findclose(hFile);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: