首先感谢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);
}