用CFile類讀取大文件
發(fā)表時(shí)間:2024-02-10 來源:明輝站整理相關(guān)軟件相關(guān)文章人氣:
[摘要]隨著Windows 2000和XP的普及,現(xiàn)在的大文件越來越多,而VC6中MFC的CFile類只支持不大于4GB的文件, 原因在于CFile類中使用了32位整型來處理文件,32位數(shù)的范圍是2的32次方(4GB),超過這個(gè)范圍的文件CFile就管不了,微軟.Net中VC7的CFile類支持大于4GB...
隨著Windows 2000和XP的普及,現(xiàn)在的大文件越來越多,而VC6中MFC的CFile類只支持不大于4GB的文件, 原因在于CFile類中使用了32位整型來處理文件,32位數(shù)的范圍是2的32次方(4GB),超過這個(gè)范圍的文件CFile就管不了,微軟.Net中VC7的CFile類支持大于4GB的文件,而.Net還不普及,開發(fā)桌面應(yīng)用VC6還是首選,所以我們可以參照VC7寫一個(gè)CFile的繼承類CFile64,使它支持大于4GB的文件:
class CFile64 : public CFile
{
public:
// Attributes
ULONGLONG GetPosition();
// Overridables
virtual ULONGLONG Seek(LONGLONG lOff, UINT nFrom);
virtual void SetLength(ULONGLONG dwNewLen);
ULONGLONG GetLength() ;
virtual void LockRange(ULONGLONG dwPos, ULONGLONG dwCount);
virtual void UnlockRange(ULONGLONG dwPos, ULONGLONG dwCount);
};
#include "stdafx.h"
#include "file64.h"
////////////////////////////////////////////////////////////////////////////
// CFile64 implementation
ULONGLONG CFile64::Seek(LONGLONG lOff, UINT nFrom)
{
ASSERT_VALID(this);
ASSERT((HANDLE)m_hFile != INVALID_HANDLE_VALUE);
ASSERT(nFrom == begin nFrom == end nFrom == current);
ASSERT(begin == FILE_BEGIN && end == FILE_END && current == FILE_CURRENT);
LARGE_INTEGER liOff;
liOff.QuadPart = lOff;
liOff.LowPart = ::SetFilePointer((HANDLE)m_hFile, liOff.LowPart, &liOff.HighPart,
(DWORD)nFrom);
if (liOff.LowPart == (DWORD)-1)
if (::GetLastError() != NO_ERROR)
CFileException::ThrowOsError((LONG)::GetLastError(), m_strFileName);
return liOff.QuadPart;
}
ULONGLONG CFile64::GetPosition()
{
ASSERT_VALID(this);
ASSERT((HANDLE)m_hFile != INVALID_HANDLE_VALUE);
LARGE_INTEGER liPos;
liPos.QuadPart = 0;
liPos.LowPart = ::SetFilePointer((HANDLE)m_hFile, liPos.LowPart, &liPos.HighPart , FILE_CURRENT);
if (liPos.LowPart == (DWORD)-1)
if (::GetLastError() != NO_ERROR)
CFileException::ThrowOsError((LONG)::GetLastError(), m_strFileName);
return liPos.QuadPart;
}
void CFile64::LockRange(ULONGLONG dwPos, ULONGLONG dwCount)
{
ASSERT_VALID(this);
ASSERT((HANDLE)m_hFile != INVALID_HANDLE_VALUE);
ULARGE_INTEGER liPos;
ULARGE_INTEGER liCount;
liPos.QuadPart = dwPos;
liCount.QuadPart = dwCount;
if (!::LockFile((HANDLE)m_hFile, liPos.LowPart, liPos.HighPart, liCount.LowPart,
liCount.HighPart))
{
CFileException::ThrowOsError((LONG)::GetLastError(), m_strFileName);
}
}
void CFile64::UnlockRange(ULONGLONG dwPos, ULONGLONG dwCount)
{
ASSERT_VALID(this);
ASSERT((HANDLE)m_hFile != INVALID_HANDLE_VALUE);
ULARGE_INTEGER liPos;
ULARGE_INTEGER liCount;
liPos.QuadPart = dwPos;
liCount.QuadPart = dwCount;
if (!::UnlockFile((HANDLE)m_hFile, liPos.LowPart, liPos.HighPart, liCount.LowPart,
liCount.HighPart))
{
CFileException::ThrowOsError((LONG)::GetLastError(), m_strFileName);
}
}
void CFile64::SetLength(ULONGLONG dwNewLen)
{
ASSERT_VALID(this);
ASSERT((HANDLE)m_hFile != INVALID_HANDLE_VALUE);
Seek(dwNewLen, (UINT)begin);
if (!::SetEndOfFile((HANDLE)m_hFile))
CFileException::ThrowOsError((LONG)::GetLastError(), m_strFileName);
}
ULONGLONG CFile64::GetLength()
{
ASSERT_VALID(this);
ULARGE_INTEGER liSize;
liSize.LowPart = ::GetFileSize((HANDLE)m_hFile, &liSize.HighPart);
if (liSize.LowPart == (DWORD)-1)
if (::GetLastError() != NO_ERROR)
CFileException::ThrowOsError((LONG)::GetLastError(), m_strFileName);
return liSize.QuadPart;
}
/////////////////////////////////////////////////////////////////////////////
LONGLONG是64位整型,這樣在理論上可支持的最大文件為18000000000GB,你也可以根據(jù)自己的需要重載CFile的其他函數(shù)。