首 页IT知识库翔宇问吧收藏本站
当前位置:翔宇亭IT乐园IT知识库编程技术C/C++

使用CFile类读取大文件

减小字体 增大字体 作者:不详  来源:转载  发布时间:2010-08-05 11:05:00

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


本文源自:翔宇亭——IT乐园(http://www.biye5u.com),转载请保留此信息!

知识评论评论内容只代表网友观点,与本站立场无关!

   评论摘要(共 0 条,得分 0 分,平均 0 分) 查看完整评论

用户名: 查看更多评论

分 值:100分 85分 70分 55分 40分 25分 10分 1分

内 容:

            请注意用语文明且合法,不要发布带有攻击性、侮辱性的言论,谢谢合作!

         通知管理员 验证码:

关于本站 | 网站帮助 | 广告合作 | 网站声明 | 友情连接 | 网站地图 | 用户守则 | 联系我们 |
本站大多数内容来自互联网或网站会员发布,如有侵权,请来信告之,谢谢!
Copyright © 2007-2017 biye5u.com. All Rights Reserved.
网站备案号:黑ICP备13005378号-3