用CFile类读取大文件

类别:VC语言 点击:0 评论:0 推荐:

随着Windows 2000和XP的普及,现在的大文件越来越多,而VC6中MFC的CFile类只支持不大于4GB的文件, 原因在于CFile类中使用了32位整型来处理文件,32位数的范围是2的32次方(4GB),超过这个范围的文件CFile就管不了,微软.Net中VC7的CFile类支持大于4GB的文件,而.Net还不普及,开发桌面应用VC6还是首选,所以我们可以参照VC7写一个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,你也可以根据自己的需要重载CFile的其他函数。

本文地址:http://com.8s8s.com/it/it374.htm