遍历特定目录,以便分别处理其中的文件?解决方法

遍历特定目录,以便分别处理其中的文件?
在C++中,怎么样遍历特定目录,以便分别处理其中的文件?

------解决方案--------------------
摘了网上一段代码:很容易理解
int SearchDirectory(std::vector <std::string> &refvecFiles,
const std::string &refcstrRootDirectory,
const std::string &refcstrExtension,
bool bSearchSubdirectories = true)
{
std::string strFilePath; // Filepath
std::string strPattern; // Pattern
std::string strExtension; // Extension
HANDLE hFile; // Handle to file
WIN32_FIND_DATA FileInformation; // File information


strPattern = refcstrRootDirectory + "\\*.* ";

hFile = ::FindFirstFile(strPattern.c_str(), &FileInformation);
string temp(FileInformation.cFileName);
if(hFile != INVALID_HANDLE_VALUE)
{
do
{
if(FileInformation.cFileName[0] != '. ')
{
strFilePath.erase();
strFilePath = refcstrRootDirectory + "\\ " + temp;

if(FileInformation.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
{
if(bSearchSubdirectories)
{
// Search subdirectory
int iRC = SearchDirectory(refvecFiles,
strFilePath,
refcstrExtension,
bSearchSubdirectories);
if(iRC)
return iRC;
}
}
else
{
// Check extension
strExtension = FileInformation.cFileName;
strExtension = strExtension.substr(strExtension.rfind( ". ") + 1);

if(strExtension == refcstrExtension)
{
// Save filename
refvecFiles.push_back(FileInformation.cFileName);
}
}
}
}
while(::FindNextFile(hFile, &FileInformation) == TRUE);

// Close handle
::FindClose(hFile);

DWORD dwError = ::GetLastError();
if(dwError != ERROR_NO_MORE_FILES)
return dwError;
}
return 0;
}
------解决方案--------------------
class Cbrowse
{
public:
Cbrowse();
bool SetInitDir(char* szdir);

void BeginBrowse();

DWORD dwfilenum;
DWORD dwdirnum;
char szSetInitDir[MAX_PATH];

protected:
bool BrowseDir(char* dir);
};

Cbrowse::Cbrowse()
{
dwfilenum = 0;
dwdirnum = 0;
}

bool Cbrowse::SetInitDir(char* szdir)
{
if (chdir(szdir) != 0)
{
return FALSE;
}
if (_fullpath(szSetInitDir, szdir, MAX_PATH)!=NULL)
{
int dwLenOfInitDir;
strcmp(szSetInitDir, szdir);
dwLenOfInitDir = strlen(szSetInitDir);
if (szSetInitDir[dwLenOfInitDir - 1] != '\\ ')
{
strcat(szSetInitDir, "\\* ");
}
else
{
strcat(szSetInitDir, "* ");
}
return TRUE;
}
return FALSE;
}

void Cbrowse::BeginBrowse()
{
Cbrowse::BrowseDir(szSetInitDir);
}

bool Cbrowse::BrowseDir(char* dir)
{
HANDLE hFile;
WIN32_FIND_DATAA FindData;
hFile = FindFirstFile(dir, &FindData);

if (hFile != INVALID_HANDLE_VALUE)
{
do
{
if (FindData.dwFileAttributes != FILE_ATTRIBUTE_DIRECTORY)
{
dwfilenum++;
}
}while (FindNextFile(hFile, &FindData));
}

hFile = FindFirstFile(dir, &FindData);
if (hFile != INVALID_HANDLE_VALUE)