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

📄 fileopt.cpp

📁 设计模式:工厂模式、单例模式的基本实现
💻 CPP
字号:
#include "FileOpt.h"

CFileOpt::CFileOpt()
{
}

CFileOpt::~CFileOpt()
{
}

int CFileOpt::Init(string strFileName, bool bRead, bool bBinary)
{
	m_strFileName = strFileName;
	m_bRead = bRead;
	int nFormat = m_bRead ? ios_base::in : ios_base::out;
	if(bBinary)
	{
		nFormat |= ios_base::binary;
	}
	m_pFile.open(m_strFileName.c_str(), nFormat);

	if(m_pFile.fail())
	{
		return 1;
	}
	return 0;
}

void CFileOpt::FInit()
{
	m_pFile.close();
}

CFileOpt* CFileOpt::NewObj(string strFileName, bool bRead, bool bBinary)
{
	CFileOpt *pObj = new CFileOpt;
	if(pObj->Init(strFileName, bRead, bBinary) == 1)
	{
		delete pObj;
		return NULL;
	}
	return pObj;
}

void CFileOpt::DelObj(CFileOpt* pObj)
{
	pObj->FInit();
	delete pObj;
}


CFileOpt& CFileOpt::operator<<(int nIn)
{
	m_pFile.write((const char*)&nIn, sizeof(nIn));
	return *this;
}

CFileOpt& CFileOpt::operator<<(float fIn)
{
	m_pFile.write((const char*)&fIn, sizeof(fIn));
	return *this;
}

CFileOpt& CFileOpt::operator<<(double dIn)
{
	m_pFile.write((const char*)&dIn, sizeof(dIn));
	return *this;
}

CFileOpt& CFileOpt::operator<<(char chIn)
{
	m_pFile.write((const char*)&chIn, sizeof(chIn));
	return *this;
}

CFileOpt& CFileOpt::operator<<(string strIn)
{
	int nNum = strIn.length();
	*this << nNum;
	for(int i=0; i<nNum; ++i)
	{
		*this << strIn[i];
	}
	return *this;
}

CFileOpt& CFileOpt::operator>>(int &nIn)
{
	m_pFile.read((char*)&nIn, sizeof(nIn));
	return *this;
}

CFileOpt& CFileOpt::operator>>(float &fIn)
{
	m_pFile.read((char*)&fIn, sizeof(fIn));
	return *this;
}

CFileOpt& CFileOpt::operator>>(double &dIn)
{
	m_pFile.read((char*)&dIn, sizeof(dIn));
	return *this;
}

CFileOpt& CFileOpt::operator>>(char &chIn)
{
	m_pFile.read((char*)&chIn, sizeof(chIn));
	return *this;
}

CFileOpt& CFileOpt::operator>>(string &strIn)
{
	int nNum = 0;
	*this >> nNum;
	for(int i=0; i<nNum; ++i)
	{
		char ch;
		*this >> ch;
		strIn += ch;
	}
	return *this;
}

int CFileOpt::WriteLine(string strText)
{
	m_pFile.write(strText.c_str(), strText.length());
	m_pFile.write("\r\n", 2);
	return 0;
}

⌨️ 快捷键说明

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