MFC遍历文件夹

tmwanly 2013-03-26 10:34:59

void dfsFolder(string folderPath, ofstream &fout)
{
_finddata_t FileInfo;
string strfind = folderPath + "\\*";
long Handle = _findfirst(strfind.c_str(), &FileInfo);

if (Handle == -1L)
{
cerr << "can not match the folder path" << endl;
exit(-1);
}
do{
//判断是否有子目录
if (FileInfo.attrib & _A_SUBDIR)
{
if( (strcmp(FileInfo.name,".") != 0 ) &&(strcmp(FileInfo.name,"..") != 0))
{
string newPath = folderPath + "\\" + FileInfo.name;
dfsFolder(newPath, fout);
}
}
else
{
fout << folderPath << "\\" << FileInfo.name << " ";
}
}while (_findnext(Handle, &FileInfo) == 0);

_findclose(Handle);
fout.close();
}

该怎么调用呢,folderPath是文件,那fout是什么呢,起什么作用的,没看明白,求解释
...全文
159 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
赵4老师 2013-03-27
  • 打赏
  • 举报
回复
system("dir /b /a-d c:\\*.* >d:\\allfiles.txt"); //读文件d:\\allfiles.txt的内容即C:\\下所有文件的名字 system("dir /b /ad c:\\*.* >d:\\alldirs.txt"); //读文件d:\\alldirs.txt的内容即C:\\下所有子目录的名字 请记住,能用shell命令获取文件、文件夹信息或者操作文件、文件夹最好用shell命令获取或者操作,而不要用各种API获取或者操作,因为当遇到非法文件夹名或非法文件名或非法文件长度、非法文件日期、压缩文件、链接文件、稀疏文件……等各种意料之外的情况时,API会处理的不全面或陷入死循环,而shell命令不会。
tmwanly 2013-03-26
  • 打赏
  • 举报
回复
引用 6 楼 a443475601 的回复:
引用 5 楼 tmwanly 的回复:不知道怎么打开文件下的子文件 string newPath = folderPath + "\\" + FileInfo.name; dfsFolder(newPath, fout); newPath 不就是子文件夹路径了么 这个是递归调用,你应该明白的吧 fout 就是输出用的 相当于……
这个我知道这个函数是遍历文件的 但是对于函数中(string folderPath ,ofstream &fout ) 这两个值有点模糊,具体应该是什么样的值,代表什么。现在需要用它调用不好 程序直接进了if (Handle == -1L)语句。
苹果皮 2013-03-26
  • 打赏
  • 举报
回复
引用 5 楼 tmwanly 的回复:
不知道怎么打开文件下的子文件
string newPath = folderPath + "\\" + FileInfo.name; dfsFolder(newPath, fout); newPath 不就是子文件夹路径了么 这个是递归调用,你应该明白的吧 fout 就是输出用的 相当于那个cout 没有其他作用 递归遍历文件夹 都是这个思路,你好好看一下
tmwanly 2013-03-26
  • 打赏
  • 举报
回复
不知道怎么打开文件下的子文件
tmwanly 2013-03-26
  • 打赏
  • 举报
回复
引用 2 楼 a443475601 的回复:
folderPath 是你要遍历的那个文件夹 后面不带\\ 比如 你要遍历C盘的windows目录 folderPath = c:\windows ofstream &fout 这个不用解释了吧
ofstream &fout 主要是不知道这个啊··
苹果皮 2013-03-26
  • 打赏
  • 举报
回复
引用 2 楼 a443475601 的回复:
folderPath 是你要遍历的那个文件夹 后面不带\\ 比如 你要遍历C盘的windows目录 folderPath = c:\windows ofstream &fout 这个不用解释了吧
是不带\
苹果皮 2013-03-26
  • 打赏
  • 举报
回复
folderPath 是你要遍历的那个文件夹 后面不带\\ 比如 你要遍历C盘的windows目录 folderPath = c:\windows ofstream &fout 这个不用解释了吧
tmwanly 2013-03-26
  • 打赏
  • 举报
回复
folderPath是文件路径
苹果皮 2013-03-26
  • 打赏
  • 举报
回复
引用 8 楼 a443475601 的回复:
引用 7 楼 tmwanly 的回复:引用 6 楼 a443475601 的回复:引用 5 楼 tmwanly 的回复:不知道怎么打开文件下的子文件 string newPath = folderPath + "\\" + FileInfo.name; dfsFolder(newPath, fout); newPath 不就是子文件夹路径了么 这个是递……
dfsFolder(strFilePath,file); 我草 怎么老写错
苹果皮 2013-03-26
  • 打赏
  • 举报
回复
引用 7 楼 tmwanly 的回复:
引用 6 楼 a443475601 的回复:引用 5 楼 tmwanly 的回复:不知道怎么打开文件下的子文件 string newPath = folderPath + "\\" + FileInfo.name; dfsFolder(newPath, fout); newPath 不就是子文件夹路径了么 这个是递归调用,你应……
抱歉 fout解释错了 你要遍历C盘的windows 把便利的到所有文件名保存到c:\1.txt里 ofstream file; file.open(L"c:\\1.txt"); string strFilePath = "c:\\windows"; dfsFolder(strFilePath,out); 就是这样 你那个if (Handle == -1L) 看看是不是你参数传的有问题

64,642

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧