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

📄 fileop.cpp

📁 《Visual C++ 6.0实例教程》配套代码
💻 CPP
字号:
// FileOP.cpp: implementation of the CFileOP class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "FileExample.h"
#include "FileOP.h"

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

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

CFileOP::CFileOP()
{

}

CFileOP::~CFileOP()
{

}

BOOL CFileOP::Copy(CString strSourcePath, CString strTargetPath, int nType)
{
	switch (nType)
	{
	case COPY_CFILE: return CopyByCFile(strSourcePath, strTargetPath);
	case COPY_API:	return CopyByApi(strSourcePath, strTargetPath);
	}

	return false;
}

BOOL CFileOP::CopyByCFile(CString strSourcePath, CString strTargetPath)
{
	CFile fSource, fTarget;
	//定义4k字节的缓冲区
	char	c[4096];      
	int	nCount;  
	//打开文件
	if  (!fSource.Open(strSourcePath, CFile::modeRead))
	{
		AfxMessageBox("Open Source File Fail!");
		return  false;
	}
	if  (!fTarget.Open(strTargetPath, CFile::modeCreate | CFile::modeWrite))	
	{
		AfxMessageBox("Create Target File Fail!");
		return  false;
	}

	//读文件到缓冲区c
	nCount = fSource.Read(c, 4096);
	while (nCount)
	{
		fTarget.Write(c, nCount);
		nCount = fSource.Read(c, 4096);
	}
	fSource.Close();
	fTarget.Close();

	return true;
}

BOOL CFileOP::CopyByApi(CString strSourcePath, CString strTargetPath)
{
	CFileFind f;
	SHFILEOPSTRUCT	fop;

	memset(&fop, 0x00, sizeof(SHFILEOPSTRUCT));
	if (!f.FindFile(strSourcePath))
		return false;

	strSourcePath += '\0';	
	fop.hwnd = NULL;
	fop.pFrom = strSourcePath;
	fop.pTo = strTargetPath;
	fop.wFunc = FO_COPY;
	fop.fFlags = FOF_SILENT | FOF_NOCONFIRMATION; 

	return !::SHFileOperation(&fop); 
}

⌨️ 快捷键说明

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