// MyFile.cpp: implementation of the CMyFile class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "MyFile.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CMyFile::CMyFile()
{
m_strFilePath = "";
m_EndFilePosition = 0;
m_Mode = false;
}
CMyFile::~CMyFile()
{
}
////////////////////////////////////////////////////////////////////////////////////////////////
//函数名称:OPenFile
//功能:打开文件
//入口参数:
//strPath:文件路径
//Mode:打开文件模式 1:以二进制写模式打开一个文件,如果目标文件存在,则删除原文件
// 2:以二进制只读模式打开一个文件
// 3:以二进制读写模式打开一个文件
//出口参数:BOOL 打开成功反回true,否则返回false
//编写人:高修望
//完成日期:2006-8-4
BOOL CMyFile::OPenFile(CString strPath, int Mode)
{
m_strFilePath = strPath;
m_Mode = Mode;
if (m_Mode == 1)
{
if(m_fileObject.Open(strPath,CFile::modeWrite | CFile::modeCreate | CFile::typeBinary))
{
m_IsOpen = true;
return true;
}
}
else if(m_Mode == 2)
{
if(m_fileObject.Open(strPath,CFile::modeRead| CFile::typeBinary))
{
m_EndFilePosition = m_fileObject.SeekToEnd();
m_fileObject.SeekToBegin();
m_IsOpen = true;
return true;
}
}
else if (m_Mode == 3)
{
if(m_fileObject.Open(strPath,CFile::modeReadWrite | CFile::typeBinary))
{
m_EndFilePosition = m_fileObject.SeekToEnd();
m_fileObject.SeekToBegin();
m_IsOpen = true;
return true;
}
}
return false;
}
////////////////////////////////////////////////////////////////////////////////////////////////
//函数名称:ReadFile
//功能:读文件
//入口参数:
//pBuff:接收从文件读取数据的缓存
//lSize:从文件读取数据的大小(字节)
//出口参数:void (无)
//编写人:高修望
//完成日期:2006-8-4
void CMyFile::ReadFile(void *pBuff, long lSize)
{
if (!m_IsOpen )
{
TRACE("文件为读模式");
return ;
}
if (m_Mode == 2 || m_Mode == 3)
{
m_fileObject.Read(pBuff,lSize);
}
else
{
TRACE("文件为读模式");
}
}
////////////////////////////////////////////////////////////////////////////////////////////////
//函数名称:WriteFile
//功能:写文件
//入口参数:
//pBuff:写入文件数据的缓存
//lSize:写入文件数据的大小(字节)
//出口参数:void (无)
//编写人:高修望
//完成日期:2006-8-4
void CMyFile::WriteFile(void *pBuff, long lSize)
{
if (!m_IsOpen )
{
TRACE("文件为读模式");
return ;
}
//if (m_bMode)
{
m_fileObject.Write(pBuff,lSize);
}
/* else
{
TRACE("文件为写模式");
}
*/
}
////////////////////////////////////////////////////////////////////////////////////////////////
//函数名称:CloseFile
//功能:关闭文件
//入口参数:无
//出口参数:void (无)
//编写人:高修望
//完成日期:2006-8-4
void CMyFile::CloseFile()
{
m_IsOpen = false;
m_fileObject.Close();
}
////////////////////////////////////////////////////////////////////////////////////////////////
//函数名称:MovePointerUpFromCurrent
//功能:将文件指针从当前位置向上移ByteCount个字节
//入口参数:
//ByteCount:移动的大小(字节)
//出口参数:long 移动后的位置(字节)
//编写人:高修望
//完成日期:2006-8-4
long CMyFile::MovePointerUpFromCurrent(DWORD ByteCount)
{
return m_fileObject.Seek(0 - ByteCount,CFile::current);
}
////////////////////////////////////////////////////////////////////////////////////////////////
//函数名称:MovePointerAlowFromCurrent
//功能:将文件指针从当前位置下移ByteCount个字节
//入口参数:
//ByteCount:移动的大小(字节)
//出口参数:long 移动后的位置(字节)
//编写人:高修望
//完成日期:2006-8-4
long CMyFile::MovePointerAlowFromCurrent(DWORD ByteCount)
{
return m_fileObject.Seek(ByteCount,CFile::current);
}
////////////////////////////////////////////////////////////////////////////////////////////////
//函数名称:MovePointerAlowFromBegin
//功能:将文件指针从头向下移ByteCount个字节
//入口参数:
//ByteCount:移动的大小(字节)
//出口参数:long 移动后的位置(字节)
//编写人:高修望
//完成日期:2006-8-4
long CMyFile::MovePointerAlowFromBegin(DWORD ByteCount)
{
return m_fileObject.Seek(ByteCount,CFile::begin);
}
////////////////////////////////////////////////////////////////////////////////////////////////
//函数名称:MovePointerUpFromEnd
//功能:将文件指针从尾向上移ByteCount个字节
//入口参数:
//ByteCount:移动的大小(字节)
//出口参数:long 移动后的位置(字节)
//编写人:高修望
//完成日期:2006-8-4
long CMyFile::MovePointerUpFromEnd(DWORD ByteCount)
{
return m_fileObject.Seek(0 - ByteCount,CFile::end);
}
////////////////////////////////////////////////////////////////////////////////////////////////
//函数名称:MovePointerToBegin
//功能:将文件指针移到开始
//入口参数:无
//出口参数:void (无)
//编写人:高修望
//完成日期:2006-8-4
void CMyFile::MovePointerToBegin()
{
m_fileObject.SeekToBegin();
}
////////////////////////////////////////////////////////////////////////////////////////////////
//函数名称:MovePointerToEnd
//功能:将文件指针移到最后
//入口参数:无
//出口参数:void(无)
//编写人:高修望
//完成日期:2006-8-4
void CMyFile::MovePointerToEnd()
{
m_fileObject.SeekToEnd();
}
////////////////////////////////////////////////////////////////////////////////////////////////
//函数名称:IsOpen
//功能:文件是否己经打开
//入口参数:无
//出口参数:BOOL 如打开为true,否则为false
//编写人:高修望
//完成日期:2006-8-4
BOOL CMyFile::IsOpen()
{
return m_IsOpen;
}
////////////////////////////////////////////////////////////////////////////////////////////////
//函数名称:IsEndFile
//功能:文件指针是到移到最后
//入口参数:无
//出口参数:BOOL 是移到最后为true,否则为false
//编写人:高修望
//完成日期:2006-8-4
BOOL CMyFile::IsEndFile()
{
if (m_EndFilePosition == m_fileObject.GetPosition())
{
return true;
}
else
{
return false;
}
}
////////////////////////////////////////////////////////////////////////////////////////////////
//函数名称:DeleteFile
//功能:删除文件
//入口参数
//strPath:目标文件路径
//出口参数:BOOL 是移到最后为true,否则为false
//编写人:高修望
//完成日期:2006-8-4
BOOL CMyFile::DeleteFile(CString strPath)
{
if (strPath == "")
{
::DeleteFile(m_strFilePath);
}
else
{
::DeleteFile(strPath);
}
return TRUE;
}
////////////////////////////////////////////////////////////////////////////////////////////////
//函数名称:GetDefaultPath
//功能:取默认路径
//入口参数
//出口参数:CString 路径
//编写人:高修望
//完成日期:2006-8-4
CString CMyFile::GetDefaultPath()
{
return m_strFilePath;
}
////////////////////////////////////////////////////////////////////////////////////////////////
//函数名称:FindFile
//功能:查找文件
//strFileName:文件名
//strPath:路径,如strPath为空,那么用默认路径
//出口参数:BOOL 找到为true,否则为false
//编写人:高修望
//完成日期:2006-8-4
BOOL CMyFile::FindFile(CString strFileName,CString strPath)
{
CString strPathTemp = "";
m_hFindFile = NULL;
if(strPath == "")
{
if (m_strFilePath == "")
{
AfxMessageBox("您输入的路径有误!");
return false;
}
strPathTemp = m_strFilePath + "\\" + strFileName;
}
else
{
strPathTemp = strPath + "\\" + strFileName;
}
m_hFindFile = FindFirstFile(m_strFilePath + "\\" + strFileName,&m_FileMsg );
if (m_hFindFile == NULL)
{
return false;
}
FindClose(m_hFindFile);
return true;
}
//设置默认路径
////////////////////////////////////////////////////////////////////////////////////////////////
//函数名称:SetDefaultPath
//功能:设置默认路径
//strPath:路径
//出口�
评论1