为什么递归寻找文件名的程序在64位环境中会崩溃,32位却没有问题,有没有解决方法?

Ghotsxiu 2017-10-16 07:48:04
程序如下

#include <string>
#include <io.h>
#include <vector>
#include <iostream>
using namespace std ;

void getFiles( string path, string exd, vector<string>& files )
{
//文件句柄
long hFile = 0;
//文件信息
struct _finddata_t fileinfo;
string pathName, exdName;

if (0 != strcmp(exd.c_str(), ""))
{
exdName = "\\*." + exd;
}
else
{
exdName = "\\*";
}

if((hFile = _findfirst(pathName.assign(path).append(exdName).c_str(),&fileinfo)) != -1)
{
do
{
//如果是文件夹中仍有文件夹,迭代之
//如果不是,加入列表
if((fileinfo.attrib & _A_SUBDIR))
{
if(strcmp(fileinfo.name,".") != 0 && strcmp(fileinfo.name,"..") != 0)
getFiles( pathName.assign(path).append("\\").append(fileinfo.name), exd, files );
}
else
{
if(strcmp(fileinfo.name,".") != 0 && strcmp(fileinfo.name,"..") != 0)
files.push_back(pathName.assign(path).append("\\").append(fileinfo.name));
}
}while(_findnext(hFile, &fileinfo) == 0);
_findclose(hFile);
}
}

int main()
{
string filePath = "D:\\";
vector<string> files;

//获取该路径下的所有jpg文件
getFiles(filePath, "jpg", files);

int size = files.size();
for (int i = 0;i < size;i++)
{
cout<<files[i].c_str()<<endl;
}
return 0;
}

...全文
241 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
赵4老师 2017-11-13
  • 打赏
  • 举报
回复
system("dir /b /a-d c:\\*.* >d:\\allfiles.txt"); //读文件d:\\allfiles.txt的内容即C:\\下所有文件的名字 system("dir /b /a-d /s c:\\*.* >d:\\allfilesinsub.txt"); //读文件d:\\allfilesinsub.txt的内容即C:\\下所有文件的名字包含子目录 system("dir /b /ad c:\\*.* >d:\\alldirs.txt"); //读文件d:\\alldirs.txt的内容即C:\\下所有子目录的名字 请记住,能用shell命令获取文件、文件夹信息或者操作文件、文件夹最好用shell命令获取或者操作,而不要用各种API获取或者操作,因为当遇到非法文件夹名或非法文件名或非法文件长度、非法文件日期、压缩文件、链接文件、稀疏文件……等各种意料之外的情况时,API会处理的不全面或陷入死循环,而shell命令不会。 如果嫌system黑窗口一闪,将system("...")替换为WinExec("cmd /c ...",SW_HIDE);
lonelyhacker 2017-11-09
  • 打赏
  • 举报
回复
有可能是递归导致的内存溢出。
「已注销」 2017-11-09
  • 打赏
  • 举报
回复
intptr_t 不能用 long 替换的,还有你写的太啰嗦了。如果是 . 或 .. 必然是带文件夹属性的,这种情况没必要写两次。还有拼接查找到的文件(或文件夹)一次就够了:
#include <string>
#include <io.h>
#include <vector>
#include <iostream>
using namespace std;
 
void getFiles( string path, string ext, vector<string> &files )
{
	string find(path);
	if (ext.size() > 0)
	{
		find += "\\*." + ext;
	}
	else
	{
		find += "\\*";
	}
	struct _finddata_t fileinfo;
	intptr_t hf = _findfirst(find.c_str(),&fileinfo);
	if (hf !=  -1)
	{
		do {
			if ((fileinfo.name == ".") || (fileinfo.name == ".."))
			{
				continue;
			}
			string file(path);
			file += "\\" + fileinfo.name;
			if((fileinfo.attrib &  _A_SUBDIR))
			{
				getFiles(file, ext, files);
			}
			else
			{
				files.push_back(file);
			}
		} while (_findnext(hf, &fileinfo) == 0);
		_findclose(hf);
	}
}
 
int main()
{
	string filePath = "D:\\";
	vector<string> files;
 
	//获取该路径下的所有jpg文件
	getFiles(filePath, "jpg", files);
 
	int size = files.size();
	for (int i = 0;i < size;i++)
	{
		cout<<files[i].c_str()<<endl;
	}
	return 0;
}
smwhotjay 2017-10-17
  • 打赏
  • 举报
回复
崩溃, 建立crashdump 或者输出log 查看哪个步骤崩的。 getFiles 跟我写的读目录类似,传vector引用
赵4老师 2017-10-17
  • 打赏
  • 举报
回复
system("dir /b /a-d c:\\*.* >d:\\allfiles.txt"); //读文件d:\\allfiles.txt的内容即C:\\下所有文件的名字 system("dir /b /a-d /s c:\\*.* >d:\\allfilesinsub.txt"); //读文件d:\\allfilesinsub.txt的内容即C:\\下所有文件的名字包含子目录 system("dir /b /ad c:\\*.* >d:\\alldirs.txt"); //读文件d:\\alldirs.txt的内容即C:\\下所有子目录的名字 请记住,能用shell命令获取文件、文件夹信息或者操作文件、文件夹最好用shell命令获取或者操作,而不要用各种API获取或者操作,因为当遇到非法文件夹名或非法文件名或非法文件长度、非法文件日期、压缩文件、链接文件、稀疏文件……等各种意料之外的情况时,API会处理的不全面或陷入死循环,而shell命令不会。 如果嫌system黑窗口一闪,将system("...")替换为WinExec("cmd /c ...",SW_HIDE);
oyljerry 2017-10-17
  • 打赏
  • 举报
回复
先看出错位置,是不是有什么路径拼接处理有问题

16,472

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Web++
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

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