VC++ 文件操作

根据文件完整路径名,解析文件路径、文件名、后缀名

std::string ExtractFilePath(const char* lpszFileName)
{
	char _Drive[_MAX_DRIVE] = { 0 };
	char _Dir[_MAX_DIR] = { 0 };
	char _Filename[_MAX_FNAME] = { 0 };
	char _Ext[_MAX_EXT] = { 0 };
	char _Path[_MAX_PATH] = { 0 };
	_tsplitpath_s(lpszFileName, _Drive, _MAX_DRIVE, _Dir, _MAX_DIR, _Filename, _MAX_FNAME, _Ext, _MAX_EXT);
	sprintf_s(_Path, _MAX_PATH, "%s%s", _Drive, _Dir);
	std::string sRet = _Path;
	return sRet;
}

std::string ExtractFileName(const char* lpszFileName)
{
	char _Drive[_MAX_DRIVE] = { 0 };
	char _Dir[_MAX_DIR] = { 0 };
	char _Filename[_MAX_FNAME] = { 0 };
	char _Ext[_MAX_EXT] = { 0 };
	_tsplitpath_s(lpszFileName, _Drive, _MAX_DRIVE, _Dir, _MAX_DIR, _Filename, _MAX_FNAME, _Ext, _MAX_EXT);
	strcat_s(_Filename, _Ext);
	std::string sRet = _Filename;
	return sRet;
}

std::string ExtractFileExt(const char* lpszFileName)
{
	char _Drive[_MAX_DRIVE] = { 0 };
	char _Dir[_MAX_DIR] = { 0 };
	char _Filename[_MAX_FNAME] = { 0 };
	char _Ext[_MAX_EXT] = { 0 };
	_tsplitpath_s(lpszFileName, _Drive, _MAX_DRIVE, _Dir, _MAX_DIR, _Filename, _MAX_FNAME, _Ext, _MAX_EXT);
	std::string sRet = _Ext;
	return sRet;
}

搜索指定路径下所有文件

std::map<UINT, std::string> SearchFiles(const char* lpszFolder, const char* lpszExt, BOOL bIncludeSubFolder/* = FALSE*/)
{
	std::map<UINT, std::string> result;
	CFileFind FileFind;
	CString strFolder; strFolder.Format(_T("%s*%s"), lpszFolder, lpszExt);
	BOOL bFound = FileFind.FindFile(strFolder);
	CString strNewFolder;
	while (bFound)
	{
		bFound = FileFind.FindNextFile();
		if (FileFind.IsDots())
			continue;

		if (FileFind.IsDirectory() && bIncludeSubFolder)
		{
			strFolder.Format(_T("%s%s\\"), lpszFolder, FileFind.GetFileName());
			SearchFiles(strFolder, lpszExt, bIncludeSubFolder);
		}
		else
		{			
			std::string filename = FileFind.GetFilePath();
			std::string ext = ExtractFileExt(filename.c_str());
			if (_tcsicmp(ext.c_str(), ".png")==0 ||
				_tcsicmp(ext.c_str(), ".jpg") == 0)
				result.insert(std::make_pair(result.size() + 1, filename));
		}
	}
	FileFind.Close();
	return result;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值