赞
踩
通过C++获取文件夹中文件名以及文件路径,包括考虑子文件夹和不考虑子文件夹两种情况。
void getFiles(string path, vector <string> & files) { long hFile = 0; struct _finddata_t fileinfo; string pathp; if ((hFile = _findfirst(pathp.assign(path).append("\\*").c_str(), &fileinfo)) != -1) { do { if ((fileinfo.attrib & _A_SUBDIR)) { if (strcmp(fileinfo.name, ".") != 0 && strcmp(fileinfo.name, "..") != 0) { getFiles(pathp.assign(path).append("/").append(fileinfo.name), files); } } else { string filestr = fileinfo.name; files.push_back(filestr); } } while (_findnext(hFile, &fileinfo) == 0); _findclose(hFile); } }
void getFiles(string path, vector <string> & files) { long hFile = 0; struct _finddata_t fileinfo; string pathp; if ((hFile = _findfirst(pathp.assign(path).append("\\*").c_str(), &fileinfo)) != -1) { do { if ((fileinfo.attrib & _A_SUBDIR)) { if (strcmp(fileinfo.name, ".") != 0 && strcmp(fileinfo.name, "..") != 0) { getFiles(pathp.assign(path).append("//").append(fileinfo.name), files); } } else { string filestr = fileinfo.name; files.push_back(pathp.assign(path).append("//").append(filestr)); } } while (_findnext(hFile, &fileinfo) == 0); _findclose(hFile); } }
void getFiles(string path, vector <string> & files) { long hFile = 0; struct _finddata_t fileinfo; string pathp; if ((hFile = _findfirst(pathp.assign(path).append("\\*").c_str(), &fileinfo)) != -1) { do { if ((fileinfo.attrib & _A_SUBDIR)) { if (strcmp(fileinfo.name, ".") != 0 && strcmp(fileinfo.name, "..") != 0) { continue; } } else { string filestr = fileinfo.name; files.push_back(filestr); } } while (_findnext(hFile, &fileinfo) == 0); _findclose(hFile); } }
void getFiles(string path, vector <string> & files)
{
long hFile = 0;
struct _finddata_t fileinfo;
string pathp;
if ((hFile = _findfirst(pathp.assign(path).append("\\*").c_str(), &fileinfo)) != -1)
{
do
{
if ((fileinfo.attrib & _A_SUBDIR))
{
if (strcmp(fileinfo.name, ".") != 0 && strcmp(fileinfo.name, "..") != 0)
{
continue;
}
}
else
{
string filestr = fileinfo.name;
files.push_back(pathp.assign(path).append("//").append(filestr));
}
} while (_findnext(hFile, &fileinfo) == 0);
_findclose(hFile);
}
}
int main()
{
string basedir = "data";
vector <string> files;
getFiles(basedir, files);
int filenum = files.size();
for (int i = 0; i < filenum; i++)
{
cout << files[i] << endl;
}
system("pause");
return 0;
}
data文件夹
result文件夹
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。