windows下,c /c++实现磁盘扫描,结合配置文件,读取特定后缀文件目录代码

简介: #include #include #include #include #include "stdio.h"#include "OperatingIni.h"using namespace std;class ScanDisk //磁盘搜索类{public: Sc...
#include <windows.h>
#include <iostream>
#include <fstream> 
#include <locale.h>
#include "stdio.h"
#include "OperatingIni.h"

using namespace std;

class ScanDisk //磁盘搜索类
{
public:

	ScanDisk(TCHAR *Expansion,TCHAR *FileName);//构造函数
	~ScanDisk();

   	TCHAR DriveString[MAX_PATH];// 驱动器列表
	TCHAR Driver[MAX_PATH];//驱动器名
	TCHAR Expansion[MAX_PATH];//后缀名
	TCHAR FileName[MAX_PATH];//构造函数使用生成的文件名
	TCHAR Name[MAX_PATH];//还未传送的文件路径
	TCHAR ConfigName[MAX_PATH];//要使用的配置文件名
	DWORD count;//文件个数
	DWORD Transform_count;//已传送文件个数

	CIniReader *Reader;
	CIniWriter *Writer;
	FILE *fp;//文件指针,创建路径文件
public:

TCHAR * GetFirstFile();//得到第一个文件路径
//void ModifyPath(TCHAR *path);//修改路径字符串
void SearchforAllDriver(); //搜索所有驱动器
void GetDriverList();//得到驱动器列表
bool Search(TCHAR *Path,TCHAR *File);//递归搜索
bool TcharMarch(TCHAR *fileName,TCHAR *Extension);//文件后缀名匹配
void SetExpansion(TCHAR *Expansion);//设置新的文件后缀
void SetConfigName(TCHAR *ConfigName);//设置需要操作的配置文件名
void InitOperateIni(TCHAR *ConfigName);//初始化配置信息类
void GetAllExpansion();//得到所有后缀名并且检索目录写入文件
};

ScanDisk::ScanDisk(TCHAR *Expansion,TCHAR *FileName)//初始化工作
{
	memset(this->DriveString,0,sizeof(this->DriveString));
	memset(this->Driver,0,sizeof(this->Driver));
	memset(this->Expansion,0,sizeof(this->Expansion));
	memset(this->FileName,0,sizeof(this->FileName));
	memset(this->Name,0,sizeof(this->Name));
	memset(this->ConfigName,0,sizeof(this->ConfigName));
	this->count=0;//文件个数
	this->Transform_count=0;//已传送文件个数为0

	memcpy(this->Expansion,Expansion,wcslen(Expansion)*2);
	memcpy(this->FileName,FileName,wcslen(FileName)*2);
	//MessageBox(NULL,this->FileName,NULL,MB_OK);
//	MessageBox(NULL,this->Expansion,NULL,MB_OK);

}

ScanDisk::~ScanDisk()
{
	fclose(this->fp);

}

void ScanDisk::SetExpansion(TCHAR *Expansion)
{
	memset(this->Expansion,0,sizeof(this->Expansion));
	memcpy(this->Expansion,Expansion,wcslen(Expansion)*2);
}

void ScanDisk::SetConfigName(TCHAR *ConfigName)
{
	memset(this->ConfigName,0,sizeof(this->ConfigName));
	memcpy(this->ConfigName,ConfigName,wcslen(ConfigName)*2);
}

void ScanDisk::InitOperateIni(TCHAR *ConfigName)
{
	memset(this->ConfigName,0,sizeof(this->ConfigName));
	memcpy(this->ConfigName,ConfigName,wcslen(ConfigName)*2);
	this->Writer=new CIniWriter(this->ConfigName);
	this->Reader=new CIniReader(this->ConfigName);
	(this->Writer)->WriteInteger(L"Setting",L"count",this->count);
	(this->Writer)->WriteInteger(L"Setting",L"Transform_count",this->Transform_count);
}

void ScanDisk::GetAllExpansion()//读取配置文件中的每一个后缀名,遍历磁盘写入文件
{
	TCHAR *expansion=(this->Reader)->ReadString(L"Setting", L"extension", L"");//此处设计不是很好
	int length=lstrlen(expansion)+1;//没有斜杠零
	int i=0;
	TCHAR temp[MAX_PATH]={0};
	for (int j=0;j<length;j++)
	{

		if (((*expansion)!=L',')&&((*expansion)!=L'\0'))
		{
			memcpy(&temp[i],expansion,sizeof(TCHAR));
			temp[i++];
			expansion++;
		}
		
		if (((*expansion)==L',')||((*expansion)==L'\0'))
		{ 

			temp[i]=L'\0';
			this->SetExpansion(temp);
			this->SearchforAllDriver();
			if ((*expansion)==L'\0')
			{
				break;
			}
			expansion++;
			i=0;
			memset(temp,0,sizeof(temp));
				
		}

	 }
	 

 
 
}


TCHAR * ScanDisk::GetFirstFile()
{
	DWORD number=(this->Reader)->ReadInteger(L"Setting",L"Transform_count",this->Transform_count);//看看读到第几个文件了
	this->fp=_wfopen(this->FileName,L"r"); //读的方式打开
	if(!this->fp) 
	{ 
		cout<<L"Can not open the .txt file"<<endl; 
	}
	else
	{
		cout<<"the file is opened !"<<endl; 
	}
	//TCHAR path[MAX_PATH]={0};
	for (int i=0;i<=number;i++)
	{
			fgetws(this->Name,MAX_PATH,this->fp);//
	}
	//fgetws(this->Name,MAX_PATH,this->fp);//
	this->Name[lstrlen(this->Name)-1]=0;//去掉文件最后的0A
	wprintf(this->Name);
	//MessageBox(NULL,this->Name,NULL,MB_OK);
	this->Transform_count++;
	(this->Writer)->WriteInteger(L"Setting",L"Transform_count",this->Transform_count);
	fclose(this->fp);
	
	return this->Name;

}

void ScanDisk::SearchforAllDriver()
{

	memset(this->Driver,0,sizeof(this->Driver));
	
	this->GetDriverList();
	int driverCount=0;
	TCHAR * pDrive= this->DriveString;
	while( *pDrive )
	{
		pDrive += wcslen( pDrive ) + 1;
		driverCount++;
		
	} 
//	printf("%d\n",driverCount);//总共几个驱动器
	pDrive= this->DriveString;
	
	this->fp=_wfopen(this->FileName,L"a+"); //追加的方式打开
	if(!this->fp) 
	{ 
		cout<<L"Can not open the .txt file"<<endl; 
	}
	else
	{
		cout<<"the file is opened !"<<endl; 
	}
	//for (int i=0;i<driverCount;i++)
	//{
	while( * pDrive )
	{
		memcpy(this->Driver,pDrive,wcslen(this->DriveString)+1);//控制字符长度,和缓冲区
		//MessageBox(NULL,this->Driver,NULL,MB_OK);
		this->Search(this->Driver,this->Expansion);
		fflush(this->fp);
		pDrive=pDrive+wcslen(pDrive)+1;
	}
	//}
	(this->Writer)->WriteInteger(L"Setting",L"count",this->count);

}

void ScanDisk::GetDriverList()
{
	TCHAR	DriveString[MAX_PATH];
	// 前一个字节为令牌,后面的52字节为驱动器跟相关属性
	GetLogicalDriveStrings(sizeof(DriveString), DriveString);
	memcpy(this->DriveString,DriveString,sizeof(this->DriveString));
}


bool ScanDisk::TcharMarch(TCHAR *fileName,TCHAR *Extension)//文件后缀名匹配
{
	int length_of_ext=wcslen(Extension);
	int length_of_name=wcslen(fileName);
	int i=0;
	while(i<length_of_ext)
	{
		if (fileName[i+(length_of_name-length_of_ext)]!=Extension[i])
		{
			return false;
		}
		else
			i++;
	}
	return true;

}

bool ScanDisk::Search(TCHAR *Path,TCHAR *File)
{
	HANDLE hFind;
	WIN32_FIND_DATA wfd;
	
	ZeroMemory(&wfd,sizeof(WIN32_FIND_DATA));
	TCHAR PathTemp[MAX_PATH];
	memset(PathTemp,0,sizeof(PathTemp));

	swprintf(PathTemp,L"%s\\*.*",Path);
	hFind=FindFirstFile(PathTemp,&wfd);

	if(INVALID_HANDLE_VALUE==hFind)
	{
		//MessageBox(NULL,L"INVALID_HANDLE_VALUE",L"FindFirstFile",MB_OK);
		return false;
	}
	
	do
	{
		if('.'==wfd.cFileName[0])
		{
			continue;
		}
		
		if(wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
		{
			swprintf(PathTemp,L"%s\\%s",Path,wfd.cFileName);
			//MessageBox(NULL,PathTemp,"Directory",MB_OK);
			//wprintf(PathTemp);
			//printf("\n");
			Search(PathTemp,File);
			fflush(this->fp);
		}
		else
		{
			
			if (TcharMarch(wfd.cFileName,File))
			{
			
				swprintf(PathTemp,L"%s\\%s",Path,wfd.cFileName);
				//MessageBox(NULL,L"Found",PathTemp,MB_OK);
				//printf(PathTemp);
				//wprintf(PathTemp);
				//printf("\n");
///////////////////////////////////////////////////////////////////////////////////
//				TCHAR temp[MAX_PATH];
//				memcpy(temp," ",sizeof(temp));
//				temp[MAX_PATH-2]=L'\0';
//				memcpy(temp,PathTemp,lstrlen(PathTemp)*2);
//////////////////////////////////////////////////////////////////////////////////
				fwprintf(this->fp,L"%s",PathTemp);
				
				fwprintf(this->fp,L"\n");
				this->count++;//文件个数加1
				
			}
		}
		
	}while(FindNextFile(hFind,&wfd));
	
	FindClose(hFind);
	
	return true;
	
}


这个代码必须在unicode工程下使用,支持中文路径,
"OperatingIni.h"//这个头文件,如下所示

 

 

#include <tchar.h>
#include <iostream>
#include <Windows.h>

class CIniReader
{
public:
	CIniReader(TCHAR * szFileName); 
	int ReadInteger(TCHAR* szSection, TCHAR* szKey, int iDefaultValue);
	//float ReadFloat(TCHAR* szSection, TCHAR* szKey, float fltDefaultValue);
	bool ReadBoolean(TCHAR* szSection, TCHAR* szKey, bool bolDefaultValue);
	TCHAR* ReadString(TCHAR* szSection, TCHAR* szKey, const TCHAR* szDefaultValue);
private:
	TCHAR m_szFileName[MAX_PATH];
};

class CIniWriter
{
public:
	CIniWriter(TCHAR* szFileName); 
	void WriteInteger(TCHAR* szSection, TCHAR* szKey, int iValue);
	//void WriteFloat(TCHAR* szSection, TCHAR* szKey, float fltValue);
	void WriteBoolean(TCHAR* szSection, TCHAR* szKey, bool bolValue);
	void WriteString(TCHAR* szSection, TCHAR* szKey, TCHAR* szValue);
	void DeleteString(TCHAR* szSection, TCHAR* szKey);
private:
	TCHAR m_szFileName[MAX_PATH];
};


CIniReader::CIniReader(TCHAR* szFileName)
{
	memset(m_szFileName, 0x00, MAX_PATH);
	memcpy(m_szFileName, szFileName, wcslen(szFileName)*2);//注意此处
}

int CIniReader::ReadInteger(TCHAR* szSection, TCHAR* szKey, int iDefaultValue)
{
	int iResult = GetPrivateProfileInt(szSection,  szKey, iDefaultValue, m_szFileName); 
	return iResult;
}

//float CIniReader::ReadFloat(TCHAR* szSection, TCHAR* szKey, float fltDefaultValue)
//{
//TCHAR szResult[255];
//TCHAR szDefault[255];
//float fltResult;
//	swprintf(szDefault, L"%f",fltDefaultValue);
//	GetPrivateProfileString(szSection,  szKey, szDefault, szResult, 255, m_szFileName); 
//	fltResult =  atof(szResult);
//	return fltResult;
//}

bool CIniReader::ReadBoolean(TCHAR* szSection, TCHAR* szKey, bool bolDefaultValue)
{
	TCHAR szResult[MAX_PATH];
	TCHAR szDefault[MAX_PATH];
	bool bolResult;
	swprintf(szDefault, L"%s", bolDefaultValue? L"True" : L"False");
	GetPrivateProfileString(szSection, szKey, szDefault, szResult, 255, m_szFileName); 
	bolResult =  (wcscmp(szResult, L"True") == 0 || 
		wcscmp(szResult, L"true") == 0) ? true : false;
	return bolResult;
}

TCHAR* CIniReader::ReadString(TCHAR* szSection, TCHAR* szKey, const TCHAR* szDefaultValue)
{
	TCHAR* szResult = new TCHAR[MAX_PATH];
	memset(szResult, 0x00, MAX_PATH);
	GetPrivateProfileString(szSection,  szKey, 
		szDefaultValue, szResult, MAX_PATH, m_szFileName); 
	return szResult;
}

CIniWriter::CIniWriter(TCHAR* szFileName)
{
	memset(m_szFileName, 0x00, MAX_PATH);
	memcpy(m_szFileName, szFileName, wcslen(szFileName)*2);
}

void CIniWriter::WriteInteger(TCHAR* szSection, TCHAR* szKey, int iValue)
{
	TCHAR szValue[MAX_PATH];
	swprintf(szValue, L"%d", iValue);
	WritePrivateProfileString(szSection,  szKey, szValue, m_szFileName); 
}

//void CIniWriter::WriteFloat(TCHAR* szSection, TCHAR* szKey, float fltValue)
//{
//TCHAR szValue[255];
//swprintf(szValue,L"%f", fltValue);
//WritePrivateProfileString(szSection,  szKey, szValue, m_szFileName); 
//}

void CIniWriter::WriteBoolean(TCHAR* szSection, TCHAR* szKey, bool bolValue)
{
	TCHAR szValue[MAX_PATH];
	swprintf(szValue, L"%s", bolValue ? L"True" : L"False");
	WritePrivateProfileString(szSection,  szKey, szValue, m_szFileName); 
}

void CIniWriter::WriteString(TCHAR* szSection, TCHAR* szKey, TCHAR* szValue)
{
	WritePrivateProfileString(szSection,  szKey, szValue, m_szFileName);
}

void CIniWriter::DeleteString(TCHAR* szSection, TCHAR* szKey)
{
	WritePrivateProfileString(szSection,szKey,NULL,m_szFileName);
}


主函数可以这么写:

#include "ScanDisk.h"
//#include "OperatingIni.h"

int main()
{
	//_wsetlocale设置中文语言环境
	_wsetlocale(LC_ALL,L"chs");
	
	
	

  //*/
	//_wsetlocale设置中文语言环境
//	_wsetlocale(LC_ALL,L"chs");
	
	CIniWriter iniWriter(L".//Log.ini");
	iniWriter.WriteString(L"Setting",L"extension", L".txt");   
	//iniWriter.WriteInteger(L"Setting", L"count", 2); 
	////iniWriter.WriteFloat(L"Setting", L"Height", 1.82f); 
	//iniWriter.WriteBoolean(L"Setting", L"Marriage", false);  
	//iniWriter.WriteBoolean(L"Setting", L"Marriage", NULL); 
	//iniWriter.DeleteString(L"Setting",L"Marriage");
	//iniWriter.WriteString("ff", "Name", "jia");   
	//	iniWriter.WriteInteger("ff", "Age", 8); 
	//iniWriter.WriteFloat("ff", "Height", 1.82f); 
	//iniWriter.WriteBoolean("ff", "Marriage", false); 
	
	CIniReader iniReader(L".//Log.ini");
	
	TCHAR *szName = iniReader.ReadString(L"Setting", L"extension", L"");   
//	int iAge = iniReader.ReadInteger(L"Setting", L"Age", 0); 
	//float fltHieght = iniReader.ReadFloat(L"Setting", L"Height", 1.80f); 
//	bool bMarriage = iniReader.ReadBoolean(L"Setting", L"Marriage", true); 
	
	//std::cout<<L"Name:"<<szName<<std::endl
	//	<<L"Age:"<<iAge<<std::endl 
	//<<L"Height:"<<fltHieght<<std::endl 
	//<<L"Marriage:"<<bMarriage<<std::endl; 
	//wprintf(szName);
//	wprintf(L"\n");
	//wprintf(L"%d\n",iAge);
    _wremove(L"Logger.log");//删除ini文件
	TCHAR *file= L"Logger.log";
	TCHAR *ext=szName;
	ScanDisk sd(ext,file);
	sd.InitOperateIni(L".//Log.ini");
	sd.GetAllExpansion();
	sd.SearchforAllDriver();
	sd.GetFirstFile();
	sd.GetFirstFile();
	//sd.SetExpansion(L".dsp");
	//sd.SearchforAllDriver();
	//sd.GetFirstFile();
//	MessageBox(NULL,sd.GetFirstFile(),NULL,MB_OK);
	


 
	//fclose(fp);
	//fclose(sd.fp);
	system("pause");
	return 0;
}

 

 

 

配置文件:

 

部分搜索结果:

资源下载:

 

http://download.csdn.net/detail/wangyaninglm/8301303

 

相关文章
|
11月前
|
安全 生物认证 网络安全
windows10无法设置默认保存位置怎么办?显示错误代码0x80070002怎么解决?
Win10系统下载文件时,默认会保存在特定位置,但用户可自行修改。若更改后仍无效,可通过删除目标磁盘中的特定文件夹、修改注册表权限、“干净启动”排除干扰软件或使用第三方修复工具等方式解决此问题。
1709 0
|
存储 算法 C++
Windows共享文件:探秘C++实现的B树索引算法奇境
在数字化时代,Windows共享文件的高效管理至关重要。B树算法以其自平衡多路搜索特性,在文件索引与存储优化中表现出色。本文探讨B树在Windows共享文件中的应用,通过C++实现具体代码,展示其构建文件索引、优化数据存储的能力,提升文件检索效率。B树通过减少磁盘I/O操作,确保查询高效,为企业和个人提供流畅的文件共享体验。
|
8月前
|
Ubuntu API C++
C++标准库、Windows API及Ubuntu API的综合应用
总之,C++标准库、Windows API和Ubuntu API的综合应用是一项挑战性较大的任务,需要开发者具备跨平台编程的深入知识和丰富经验。通过合理的架构设计和有效的工具选择,可以在不同的操作系统平台上高效地开发和部署应用程序。
320 11
|
9月前
|
C++ Windows
应用程序无法正常启动(0xc0000005)?C++报错0xC0000005如何解决?使命召唤17频频出现闪退,错误代码0xC0000005(0x0)
简介: 本文介绍了Windows应用程序出现错误代码0xc0000005的解决方法,该错误多由C++运行库配置不一致或内存访问越界引起。提供包括统一运行库配置、调试排查及安装Visual C++运行库等解决方案,并附有修复工具下载链接。
2466 1
|
存储 安全 C语言
C++ String揭秘:写高效代码的关键
在C++编程中,字符串操作是不可避免的一部分。从简单的字符串拼接到复杂的文本处理,C++的string类为开发者提供了一种更高效、灵活且安全的方式来管理和操作字符串。本文将从基础操作入手,逐步揭开C++ string类的奥秘,帮助你深入理解其内部机制,并学会如何在实际开发中充分发挥其性能和优势。
|
10月前
|
Windows
office出现0xc0000142错误?windows错误代码为0xc0000142?
office出现0xc0000142错误?windows错误代码为0xc0000142?
513 0
|
11月前
|
API 数据安全/隐私保护 C++
永久修改机器码工具, exe一机一码破解工具,软件机器码一键修改工具【c++代码】
程序实现了完整的机器码修改功能,包含进程查找、内存扫描、模式匹配和修改操作。代码使用
|
人工智能 Linux iOS开发
Burp Suite Professional 2025.2 (macOS, Linux, Windows) - Web 应用安全、测试和扫描
Burp Suite Professional 2025.2 (macOS, Linux, Windows) - Web 应用安全、测试和扫描
1006 12
Burp Suite Professional 2025.2 (macOS, Linux, Windows) - Web 应用安全、测试和扫描
|
C++
爱心代码 C++
这段C++代码使用EasyX图形库生成动态爱心图案。程序通过数学公式绘制爱心形状,并以帧动画形式呈现渐变效果。运行时需安装EasyX库,教程链接:http://【EasyX图形库的安装和使用】https://www.bilibili.com/video/BV1Xv4y1p7z1。代码中定义了屏幕尺寸、颜色数组等参数,利用随机数与数学函数生成动态点位,模拟爱心扩散与收缩动画,最终实现流畅的视觉效果。
1285 0
|
算法 安全 C++
提高C/C++代码的可读性
提高C/C++代码的可读性
419 4