由于vc6中mfc的cfile類使用32位整型數來處理文件,所以它只支持不大于4gb的文件,若超過這個范圍的文件cfile就管不了。當然,在微軟的.net中vc7的cfile類中已經支持大于4gb的文件,但我們還有必要為VC6愛好者探討一下在CFile類中支持大文件的方法。
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。
|
新聞熱點
疑難解答