首页 > 编程 > C > 正文

使用CFile类读取大文件

2023-06-09 12:08:01
字体:
来源:转载
供稿:网友

由于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。

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表