//--- CFile Implementation ---
CFile::CFile(char* lpszFileName, UINT nOpenFlags)
{
Open(lpszFileName, nOpenFlags);
}
UINT CFile::Read(void* lpBuf, UINT nCount)
{
if (nCount == 0)
return 0; // avoid Win32 "null-read"
assert(lpBuf != NULL);
if (nCount != ::fread(lpBuf, 1, nCount, m_hFile))
assert(FALSE); // fread() fail
return (UINT)nCount;
}
void CFile::Write(const void* lpBuf, UINT nCount)
{
if (nCount == 0)
return; // avoid Win32 "null-write" option
assert(lpBuf != NULL);
DWORD nWritten = ::fwrite(lpBuf, 1, nCount, m_hFile);
assert(nWritten == nCount);
}
CFile::~CFile()
{
Close();
}
void CFile::Close()
{
::fclose(m_hFile);
m_hFile = NULL;
}
BOOL CFile::Open(char* lpszFileName, UINT nOpenFlags)
{
// attempt file creation
switch (nOpenFlags)
{
case modeRead:
m_hFile = ::fopen(lpszFileName, "rt");
break;
case modeWrite:
m_hFile = ::fopen(lpszFileName, "wt");
break;
default:
assert(FALSE); // invalid share mode
}
assert(m_hFile != NULL);
return TRUE;
}
--
※ 發信站: 批踢踢實業坊(ptt.csie.ntu.edu.tw)
◆ From: 218.160.6.26