最后找到的条目数总是少于在该目录上点鼠标右键显示属性标签得到的数据.
但是将此函数应用于一些结构简单的目录(包含子目录)上就可以得到正确的结果
这说明递归逻辑应该是对的,但目录规模一大,就有问题了,这是为什么,请高手指教.请有兴趣的朋友将代码拷贝下去,调一下试试,谢谢.写这个函数的目的有两个:
一是熟悉STL的用法,唉,都学了几个月还没有什么进展.string和vector用得对不对都不知道.
二来是为了温习一下递归知识.
常时间不摸,手就生.如果有错漏,见笑,还请众前辈指点,#include <iostream.h>
#include <windows.h>
#include <vector>
#include <string>
using namespace std;int findfile(string &DirctoryPath,vector<string> &filelist)
{
WIN32_FIND_DATA FindFileData;
string tmpPath=DirctoryPath;
int result=-1;//默认返回值
::SetCurrentDirectory(DirctoryPath.c_str());//设置搜索目录路径
HANDLE hr=FindFirstFile("*.*",&FindFileData);
if(hr!=INVALID_HANDLE_VALUE)//如果DirctoryPath是一个错误路,函数将返回-1
{
while (result)
{
if (tmpPath.at(tmpPath.length()-1)=='\\') //如果是根目录遍历就特别处理
tmpPath+=FindFileData.cFileName;
else//组合完整路径
tmpPath=tmpPath+"\\"+FindFileData.cFileName;
if (FindFileData.dwFileAttributes==FILE_ATTRIBUTE_DIRECTORY &&
strcmp(FindFileData.cFileName,".") && strcmp(FindFileData.cFileName,".."))
{//如果搜到了一个不是圆点的目录,就进行递归遍历
filelist.push_back(tmpPath);//存储搜索到的目录名
findfile(tmpPath,filelist);//进行递归路径
}
if (FindFileData.dwFileAttributes==FILE_ATTRIBUTE_ARCHIVE)
filelist.push_back(tmpPath);//存储搜索到的文档名
result=FindNextFile(hr,&FindFileData);//进行下一步搜索
tmpPath=DirctoryPath;
}
}
return result;
}int main(int argc, char* argv[])
{
vector<string> pathlist;//将所有得到的路径串存储到一个向量中
string filepath="d:\\";//搜索d盘根目录
findfile(filepath,pathlist);
for(vector<string>::iterator it=pathlist.begin();it!=pathlist.end();it++)//显示所有串
cout<<(*it).c_str()<<endl;//奇怪string不支持cout的<<操作?直接cout<<*it会出错.
cout<<pathlist.size()<<endl;//显示找到了多少个文件及目录总合
return 0;
}
但是将此函数应用于一些结构简单的目录(包含子目录)上就可以得到正确的结果
这说明递归逻辑应该是对的,但目录规模一大,就有问题了,这是为什么,请高手指教.请有兴趣的朋友将代码拷贝下去,调一下试试,谢谢.写这个函数的目的有两个:
一是熟悉STL的用法,唉,都学了几个月还没有什么进展.string和vector用得对不对都不知道.
二来是为了温习一下递归知识.
常时间不摸,手就生.如果有错漏,见笑,还请众前辈指点,#include <iostream.h>
#include <windows.h>
#include <vector>
#include <string>
using namespace std;int findfile(string &DirctoryPath,vector<string> &filelist)
{
WIN32_FIND_DATA FindFileData;
string tmpPath=DirctoryPath;
int result=-1;//默认返回值
::SetCurrentDirectory(DirctoryPath.c_str());//设置搜索目录路径
HANDLE hr=FindFirstFile("*.*",&FindFileData);
if(hr!=INVALID_HANDLE_VALUE)//如果DirctoryPath是一个错误路,函数将返回-1
{
while (result)
{
if (tmpPath.at(tmpPath.length()-1)=='\\') //如果是根目录遍历就特别处理
tmpPath+=FindFileData.cFileName;
else//组合完整路径
tmpPath=tmpPath+"\\"+FindFileData.cFileName;
if (FindFileData.dwFileAttributes==FILE_ATTRIBUTE_DIRECTORY &&
strcmp(FindFileData.cFileName,".") && strcmp(FindFileData.cFileName,".."))
{//如果搜到了一个不是圆点的目录,就进行递归遍历
filelist.push_back(tmpPath);//存储搜索到的目录名
findfile(tmpPath,filelist);//进行递归路径
}
if (FindFileData.dwFileAttributes==FILE_ATTRIBUTE_ARCHIVE)
filelist.push_back(tmpPath);//存储搜索到的文档名
result=FindNextFile(hr,&FindFileData);//进行下一步搜索
tmpPath=DirctoryPath;
}
}
return result;
}int main(int argc, char* argv[])
{
vector<string> pathlist;//将所有得到的路径串存储到一个向量中
string filepath="d:\\";//搜索d盘根目录
findfile(filepath,pathlist);
for(vector<string>::iterator it=pathlist.begin();it!=pathlist.end();it++)//显示所有串
cout<<(*it).c_str()<<endl;//奇怪string不支持cout的<<操作?直接cout<<*it会出错.
cout<<pathlist.size()<<endl;//显示找到了多少个文件及目录总合
return 0;
}
第一没有现实隐藏文件
第二多包含了目录文件,其“园点”目录文件,你没有明白“&&”操作符的运行过程
这样写,因为dwFileAttributes可能是多个值的组合
应该是FindFileData.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY
即dwFileAttributes有这样的属性就行了
{
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();
cout << (LPCTSTR) str << endl;
Recurse(str);
}
} finder.Close();
}void main()
{
if (!AfxWinInit(GetModuleHandle(NULL), NULL, GetCommandLine(), 0)
cout << "panic!" << endl;
else
Recurse(_T("C:"));
}