首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

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

2012-03-08 
遍历特定目录,以便分别处理其中的文件?在C++中,怎么样遍历特定目录,以便分别处理其中的文件?[解决办法]摘

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

[解决办法]
摘了网上一段代码:很容易理解
int SearchDirectory(std::vector <std::string> &refvecFiles,
const std::string &refcstrRootDirectory,
const std::string &refcstrExtension,
bool bSearchSubdirectories = true)
{
std::stringstrFilePath; // Filepath
std::stringstrPattern; // Pattern
std::stringstrExtension; // Extension
HANDLEhFile; // Handle to file
WIN32_FIND_DATAFileInformation; // 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)


{
do
{
if (FindData.dwFileAttributes == FILE_ATTRIBUTE_DIRECTORY)
{
if (strcmp(FindData.cFileName, ". ") != 0 && strcmp(FindData.cFileName, ".. ") != 0)
{
dwdirnum++;
char szFullSubDir[MAX_PATH];
StrCpyN(szFullSubDir,dir,strlen(dir));
strcat(szFullSubDir, FindData.cFileName);

strcat(szFullSubDir, "\\* ");
//递归遍历子目录
BrowseDir(szFullSubDir);
}
}
} while(FindNextFile(hFile, &FindData));
}
FindClose(hFile);
return TRUE;
}

写的点不规范,
改的用,
个人意见,高手多多指教
[解决办法]
void SearchDir( CString dir)
{
CFileFind file;
BOOL bRet = FALSE;

if ( '\\ ' != dir[dir.GetLength()-1] )
{
dir = dir + "\\*.* ";

}

bRet = file.FindFile((LPCTSTR)dir, 0);
if(!bRet )
return;

do
{
bRet = file.FindNextFile();

if (file.IsDots())
{
}
else if (file.IsDirectory() )
{
//string strTemp = file.GetFilePath();
SearchDir(file.GetFilePath() );
}
else
{
std::cout < <file.GetFilePath() < <std::endl;
}
}while(bRet);

}
[解决办法]
#include <iostream>
#include <iterator>
#include <sstream>
#include <string>
#include <cstring>
#include <list>
#include <algorithm>
#include "windows.h "
#include "stdlib.h "
#include "Chdir.h "
#include "file.h "
//
std::list <std::string> dirs,files;
//
void path_list(const char* fname){
using namespace std;
Chdir set_path;
set_path.cd(fname);
WIN32_FIND_DATA FindFileData;
HANDLE hFind = INVALID_HANDLE_VALUE;
DWORD dwError;
//cout < < "Target directory is " < <path < < ".\n ";
char aim[MAX_PATH];
strncpy (aim, fname, strlen(fname)+1);
strncat (aim, "\\* ", 3);
hFind = FindFirstFile(aim, &FindFileData);
if (hFind == INVALID_HANDLE_VALUE)
{
cerr < < "FindFirstFile( " < <aim < < " ,&FindFileData) run error!\n ";
exit( GetLastError() );
}
else
{
//cout < < "First file name is " < <FindFileData.cFileName < < "\n ";
char s[MAX_PATH];
if( 0!=strcmp( ". ",FindFileData.cFileName) ){
_fullpath(s,FindFileData.cFileName,MAX_PATH);
files.push_back(s);
}
while (FindNextFile(hFind, &FindFileData) != 0)
{
if( 0==strcmp( ".. ",FindFileData.cFileName) ) continue;
if( FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY ){
_fullpath(s,FindFileData.cFileName,MAX_PATH);
dirs.push_back(s);
}
else{
_fullpath(s,FindFileData.cFileName,MAX_PATH);
files.push_back(s);
}
}
dwError = GetLastError();
FindClose(hFind);
if (dwError != ERROR_NO_MORE_FILES)
{
cerr < < "FindNextFile error!\n ";
exit(dwError);
}
}
}
//
void full_path_list(const char* fname){
using namespace std;
Chdir set_path;
set_path.cd(fname);


WIN32_FIND_DATA FindFileData;
HANDLE hFind = INVALID_HANDLE_VALUE;
DWORD dwError;
//cout < < "Target directory is " < <path < < ".\n ";
char aim[MAX_PATH];
strncpy (aim, fname, strlen(fname)+1);
strncat (aim, "\\* ", 3);
hFind = FindFirstFile(aim, &FindFileData);
if (hFind == INVALID_HANDLE_VALUE)
{
cerr < < "FindFirstFile( " < <aim < < " ,&FindFileData) run error!\n ";
exit( GetLastError() );
}
else
{
//cout < < "First file name is " < <FindFileData.cFileName < < "\n ";
char s[MAX_PATH];
if( 0!=strcmp( ". ",FindFileData.cFileName) ){
_fullpath(s,FindFileData.cFileName,MAX_PATH);
files.push_back(s);
}
while (FindNextFile(hFind, &FindFileData) != 0)
{
if( 0==strcmp( ".. ",FindFileData.cFileName) ) continue;
if( FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY ){
_fullpath(s,FindFileData.cFileName,MAX_PATH);
dirs.push_back(s);
full_path_list(s);
}
else{
_fullpath(s,FindFileData.cFileName,MAX_PATH);
files.push_back(s);
}
}
dwError = GetLastError();
FindClose(hFind);
if (dwError != ERROR_NO_MORE_FILES)
{
cerr < < "FindNextFile error!\n ";
exit(dwError);
}
}
}

热点排行