FindFirstFile FindNextFile实现遍历对应目录所有文件

本文介绍了一个在Windows环境下遍历文件和目录的C++程序,并实现了字符串分割、拼接及宽字节与多字节转换等功能。通过递归方式展示指定路径下所有文件名,同时探讨了内存管理中可能存在的泄漏问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

首先感谢SYC老师的免费教程 这是老师的教学地址 https://www.cctry.com/forum-147-1.html 好了 代码仅供参考

#include<windows.h>
#include<iostream>
using namespace std;
// 对字符串进行分割
char*addSplit(char*p, char c)
{
	int j = strlen(p) + 1;
	int goal = 0;
	char*newStr = NULL;
	for (int i = 0; i<j; i++)
	{
		if (p[i] == '\\')
		{
			goal = i;
		}
	}
	newStr = new char[goal + 1];
	for (int i = 0; i <= goal; i++)
	{
		if (i != goal)
		{
			newStr[i] = p[i];
		}
		else {
			newStr[i] = '\0';
		}
	}
	return newStr; //存在内存泄漏 但是在调用处 进行释放 有点 Low
}
// 对字符串进行拼接
char*addStr(char* pbefore, char*pafter)
{
	int before = strlen(pbefore);
	int  after = strlen(pafter);
	char*newStr = (char*)malloc(after + before + 1); //多分配一个字节 是字符串的结束标志
	for (int i = 0; i<after + before + 1; i++)
	{
		if (i <= before - 1)
		{
			newStr[i] = pbefore[i];
		}
		if (before - 1<i&&i <= after + before - 1)
		{
			newStr[i] = pafter[i - before];
		}

		if (i == after + before)   // 增加一个结束标志 防止出现乱码
		{
			newStr[i] = '\0';
		}
	}
	return newStr;//存在内存泄漏 但是在调用处 进行释放 有点 Low 
}
// 宽字节转多字节
char* MyselfWideCharToMulitiByte(wchar_t*wp)
{
	int length = wcslen(wp) + 1;
	char*p = new char[length * 2];
	WideCharToMultiByte(CP_ACP, 0, wp, length, p, length * 2, NULL, 0);
	return p;//存在内存泄漏 但是在调用处 进行释放 有点 Low
}
//  多字节转宽字节
wchar_t*MyselfMultiByteToWideChar(char*p)
{
	int length = strlen(p) + 1;
	wchar_t*pNew = new wchar_t[length * 2];
	MultiByteToWideChar(CP_ACP, 0, p, length, pNew, length);
	return pNew;//存在内存泄漏 但是在调用处 进行释放 有点 Low
}
void ShowFileName(wchar_t*path)
{
	WIN32_FIND_DATA File;
	HANDLE FileHandle;
	char*pName=NULL,*temp1=NULL,*temp2 = NULL,*W2A=NULL,*temp3=NULL,*temp4=NULL,*temp5=NULL;
	wchar_t*temp6 = NULL;
	FileHandle=FindFirstFile(path, &File);
	if (FileHandle != INVALID_HANDLE_VALUE)
	{
		while (FindNextFile(FileHandle,&File))
		{
			if (File.cFileName[0] == '.')
			{// 因为文件夹名不能以 . 开头
				continue;
			}
			if (File.dwFileAttributes == FILE_ATTRIBUTE_DIRECTORY)
			{// 递归 调用
				W2A= MyselfWideCharToMulitiByte(path);
				pName= MyselfWideCharToMulitiByte(File.cFileName);
				temp1 = addSplit(W2A, '\\');
				temp2 = addStr(temp1, "\\");
				temp3 = addStr(temp2, pName);
				temp4 = addStr(temp3, "\\");
				temp5 = addStr(temp4, "*.*");
				temp6 = MyselfMultiByteToWideChar(temp5);
				ShowFileName(temp6);
				delete[] W2A; W2A = NULL;
				delete[]pName; pName = NULL;
				delete[]temp1; temp1 = NULL;
				delete[]temp2; temp2 = NULL;
				delete[]temp3; temp3 = NULL;
				delete[]temp4; temp4 = NULL;
				delete[]temp5; temp5 = NULL;
				delete[]temp6; temp6 = NULL;
			}
			else {
				// 是文件 进行文件名的打印
				pName = MyselfWideCharToMulitiByte(File.cFileName);
				cout << pName<<"\n";
				delete[] pName;
				pName = NULL;
			}
		}
	}
}
int main()
{
	wchar_t*documentPath = L"c:\\*.*";
	ShowFileName(documentPath);
	Sleep(100000);
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值