⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 fileanddir.cpp

📁 使用vc++技术实现光电机串口通信管理程序
💻 CPP
字号:
// FileAndDir.cpp: implementation of the CFileAndDir class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "FileAndDir.h"
#include <io.h>
#include <Direct.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <stdlib.h>


#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CFileAndDir::CFileAndDir()
{
}

CFileAndDir::~CFileAndDir()
{

}



//********************************************************************
//	函 数 名:	IsDirExist

//	功能描述:   判断一个路径是否存在	
//	参数说明:	CString strPathName [in] 输入路径字符串
//				
//	返 回 值:	BOOL TRUE 成功, FALSE 失败
//				
//				
//	负面影响:	无
//	备    忘:	
//	已知限制:
//	错 误 号:	无
//	更新纪录:
//  日    期        姓   名                    描述
// ===========     =========  ========================================
//********************************************************************
BOOL CFileAndDir::IsDirExist(CString strPathName)
{
	CString strTempDir;

	//判断路径结尾是否有'\'
	if (strPathName.Right(1) == _T("\\"))
	{
		//去除字串尾部的'\'
		strTempDir = strPathName.Left(strPathName.GetLength() - 1);
	}
	else
	{
		strTempDir = strPathName;
	}

	strTempDir += _T("\\*.*");

	WIN32_FIND_DATA FindData;//系统查找结构
	HANDLE hFind = FindFirstFile(strTempDir,&FindData);
	
	if (hFind == INVALID_HANDLE_VALUE)//判断是否成功
	{
		return FALSE;
	}

	FindClose(hFind);
	return TRUE;

}

//********************************************************************
//	函 数 名:	RansackDirectory
////	功能描述:	遍历目录,保存遍历结果
//	参数说明:	strSoucePath 待遍历的目录
//				strListPath 
//				
//	返 回 值:	无
//				
//				
//	负面影响:	无
//	备    忘:	执行结果将遍历的文件保存到strListPath数组中
//	已知限制:	
//				
//				
//	错 误 号:	无
//	更新纪录:
//  日    期        姓   名                    描述
// ===========     =========  ========================================
//********************************************************************

void CFileAndDir::RansackDirectory(CString strSoucePath, CStringArray &strListPath)
{
	if (IsDirExist(strSoucePath) != TRUE)
		return;
	CFileFind m_Find;
	BOOL bIsFind = FALSE;
	
	if (strSoucePath != "")//如果目录不为空
	{
		bIsFind = m_Find.FindFile(strSoucePath + "\\" + "*.*");
		//保留路径
		m_strPath = strSoucePath;
	}
	while(bIsFind)//开始遍历
	{
		bIsFind = m_Find.FindNextFile();
		if(m_Find.IsDots())
			continue;
		//是否文件或目录
		if(BOOL bIsDir = m_Find.IsDirectory())
		{
			RansackDirectory(strSoucePath + "\\" + m_Find.GetFileName(),strListPath);
		}
		else
		{	
			strListPath.Add(m_Find.GetFilePath());
		}
	}
	m_Find.Close();
}
//********************************************************************
//	函 数 名:	RansackDirectoryEx
//	功能描述:	遍历目录,保存遍历结果
//	参数说明:	strSoucePath 待遍历的目录
//				strListPath 
//				iFileSum 返回遍历的文件总数
//				
//	返 回 值:	无
//				
//				
//	负面影响:	无
//	备    忘:	执行结果将遍历的文件保存到strListPath数组中
//				同时返回文件总数给iFileSum
//	已知限制:	
//				
//				
//	错 误 号:	无
//	更新纪录:
//  日    期        姓   名                    描述
// ===========     =========  ========================================
//********************************************************************

void CFileAndDir::RansackDirectoryEx(CString strSoucePath, CStringArray &strListPath, int &iFileSum)
{
	if (IsDirExist(strSoucePath) != TRUE)
		return;
	CFileFind m_Find;
	BOOL bIsFind = FALSE;
	
	if (strSoucePath != "")//如果目录不为空
	{
		bIsFind = m_Find.FindFile(strSoucePath + "\\" + "*.*");
		//保留路径
		m_strPath = strSoucePath;
	}
	while(bIsFind)//开始遍历
	{
		bIsFind = m_Find.FindNextFile();
		if(m_Find.IsDots())
			continue;
		//是否文件或目录
		if(BOOL bIsDir = m_Find.IsDirectory())
		{
			RansackDirectoryEx(strSoucePath + "\\" + m_Find.GetFileName(),strListPath,iFileSum);
		}
		else
		{	
			strListPath.Add(m_Find.GetFilePath());
			iFileSum++;
		}
	}
	m_Find.Close();
}


//********************************************************************
//	函 数 名:	AddTextToFile

//	功能描述:	追加文本内容到目标文件尾部
//	参数说明:	[in] strFileName  目标文件名
//				[in] lpcszText	  待追加的内容
//				[in] iLen		  内容的长度
//	返 回 值:	BOOL TRUE 追加成功, FALSE 追加失败
//				
//				
//	负面影响:	无
//	备    忘:	
//	已知限制:
//	错 误 号:	无
//	更新纪录:
//  日    期        姓   名                    描述
// ===========     =========  ========================================
//********************************************************************
BOOL CFileAndDir::AddTextToFile(const CString strFileName, LPCTSTR lpcszText, int iLen)
{
	BOOL bRet = FALSE;
	CFile file;

	try
	{
		//判断打开目标文件是否成功
		if (file.Open(strFileName,CFile::modeReadWrite))
		{
			//如果打开成功则进行追加
			file.SeekToEnd();
			file.Write(lpcszText, iLen);
			file.Close();

			//追加完毕
			bRet = TRUE;
		}
	}
	catch(CFileException *pe)
	{
		pe->ReportError();
	}

	return bRet;

}

//间接调用AddTextToFile成员函数
//实现同样的功能
//2002.11.27
BOOL CFileAndDir::AddTextToFile(const CString strFileName, CString strText)
{
	return AddTextToFile(strFileName, strText, strText.GetLength());
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -