直接上代码
#include <iostream>
using namespace std;
#ifdef WIN32
#include <io.h>
#else
#endif
void ReadDirectory( const string& directoryName, const string fileExt, vector<string>& filenames, bool addDirectoryName=true )
{
filenames.clear();
#ifdef WIN32
struct _finddata_t s_file;
string str = directoryName + "\\*" + fileExt;
intptr_t h_file = _findfirst( str.c_str(), &s_file );
if( h_file != static_cast<intptr_t>(-1.0) )
{
do
{
if( addDirectoryName )
filenames.push_back(directoryName + "\\" + s_file.name);
else
filenames.push_back((string)s_file.name);
}
while( _findnext( h_file, &s_file ) == 0 );
}
_findclose( h_file );
#else
#endif
sort( filenames.begin(), filenames.end() );
}
int main()
{
vector<string> fileNames;
ReadDirectory("C:\\Users\\Administrator\\Desktop\\新建文件夹", ".png", fileNames);
for (int i=0; i<fileNames.size(); i++)
{
printf("%s \n", fileNames[i].c_str());
}
printf("ok \n");
system("pause");
return 0;
}