#include <io.h>
#include <iostream>
//#include <vector>
using namespace std;
int main()
{
_finddata_t FileInfo;
string FilePathStr;
char * FilePath = "d:\\Image\\tempet\\";
long Handle = _findfirst(FilePathStr.assign(FilePath).append("*.bmp").c_str(), &FileInfo);
while( _findnext( Handle, &FileInfo) == 0)
{
cout << FilePathStr.assign(FilePath).append(FileInfo.name).c_str() << endl;
}
_findclose( Handle);
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);
}
}
int main()
{
char * filePath = "D:\\Image";
vector<string> files;
////获取该路径下的所有文件
getFiles(filePath, files );
char str[30];
int size = files.size();
for (int i = 0;i < size;i++)
{
cout<<files[i].c_str()<<endl;
}
}
const char * imgFolder = "d:\\Image";
vector<string> subFolder;
_finddata_t FileInfo;
string Folder(imgFolder);
long Handle = _findfirst( Folder.append("\\").append("*").c_str(), &FileInfo);
vector<string> dir;
if(Handle == -1)
{
return -1;
}
string str(FileInfo.name);
if(str.find('.') == -1)
{
str.insert(0, string(imgFolder).append("\\"));
subFolder.push_back(str);
}
while( _findnext( Handle, &FileInfo ) == 0 )
{
str = string(FileInfo.name);
if(str.find('.') == -1)
{
str.insert(0, string(imgFolder).append("\\"));
subFolder.push_back(str);
}
}
_findclose(Handle);
for(int k=0; k<subFolder.size(); k++)
{
cout<<subFolder[k]<<endl;
}