CFileFind ff;
if (dir.Right(1) != "\\")
{
dir += "\\";
}
dir+="*.*";
BOOL bRes = ff.FindFile(dir);
while (bRes)
{
bRes = ff.FindNextFile();
if (bRes != 0)
{
if(ff.IsDirectory()&& !ff.IsDots())
{
CString path = ff.GetFilePath();
TraverseDir(path,vec);
}
else if (!ff.IsDirectory() && !ff.IsDots())
{
CString name=ff.GetFileName();
CString path = ff.GetFilePath();
vec.push_back(path);
}
}
}
MSDN上有现成的例子代码,可以参考一下:
void Recurse(LPCTSTR pstr)
{
CFileFind finder;
// build a string with wildcards
CString strWildcard(pstr);
strWildcard += _T("\\*.*");
// start working for files
BOOL bWorking = finder.FindFile(strWildcard);
while (bWorking)
{
bWorking = finder.FindNextFile();
// skip . and .. files; otherwise, we'd
// recur infinitely!
if (finder.IsDots())
continue;
// if it's a directory, recursively search it
if (finder.IsDirectory())
{
CString str = finder.GetFilePath();
TRACE(_T("%s\n"), (LPCTSTR)str);
Recurse(str);
}
}
finder.Close();
}
void PrintDirs()
{
Recurse(_T("C:"));
}
单步调试一下,先看你在什么位置出错了
已经找到问题了 ,将if (bRes != 0)删掉就行了