ala*_*ncc 1 c++ mfc ftell visual-c++ file-length
我使用的是 Visual C++ 2008。在 VC++ 2008 中,CFile支持 2^64 个大文件。所以我觉得CStdioFile也应该支持。
但是,当CStdioFile::GetLength()在大于 2GB 的文件上使用时,我得到一个CFileException,下面是代码片段:
void CTestCStdioFileDlg::OnBnClickedButton1()
{
// TODO: Add your control notification handler code here
CStdioFile MyFile;
CString strLine;
ULONGLONG uLength;
strLine = _T("This is a line.");
if (MyFile.Open(_T("C:\\Temp\\MyTest.dat"), CFile::modeCreate | CFile::modeWrite | CFile::shareExclusive | CFile::typeBinary))
{
for (UINT uIndex = 0; uIndex = 200000000; uIndex ++)
{
MyFile.WriteString(strLine);
uLength = MyFile.GetLength();
}
MyFile.Close();
}
}
Run Code Online (Sandbox Code Playgroud)
跟踪到 后CStdio::GetLength(),我发现以下代码片段将引发异常,如下所示:
nCurrent = ftell(m_pStream); -> This will return -1
if (nCurrent == -1)
AfxThrowFileException(CFileException::invalidFile, _doserrno,
m_strFileName);
Run Code Online (Sandbox Code Playgroud)
令人惊奇的是,CStdioFile仍然使用ftell代替_ftelli64来处理流。
然后我搜索文档CStdioFile,我找不到任何文档CStdioFile::GetLength,唯一相关的是https://docs.microsoft.com/en-us/cpp/mfc/reference/cstdiofile-class?view=vs-2019#seek,它要求我查看fseek文档。但是在fseek文档中,我仍然没有找到与文件大小限制相关的任何内容。
最后我找到了一个表明CStdioFile::GetLength包含错误的第三方站点:http : //www.flounder.com/msdn_documentation_errors_and_omissions.htm#CStdioFile :: GetLength,但它没有提供解决方案。
除此之外,几乎没有任何关于 2GBCStdioFile在线限制的问题或帖子。这真的很奇怪。
我尝试检查CStdioFileiN VC++ 2017的源代码,它与 2008的源代码相同。
那么在不重写整个CStdioFile类的情况下,是否有简单的解决方案?
CStdioFile 的存在主要有一个原因:它是一个带参数的构造FILE*函数。此类的目的是包装 C 运行时文件并通过CFile兼容接口公开它。该实现继承了所有 C 运行时限制,特别是 2GB 的文件大小限制。
为了解决这个问题,有几种选择:
如果可能,CStdioFile完全放弃使用。除非您与(遗留)C 代码交互,否则没有明显的理由使用它。
如果这不是一个选项,获得从自定义实现CStdioFile和override所有类成员表面文件相对偏移量(GetPosition(),GetLength(),Seek())。所有其他类成员不受影响,可以简单地继承。(注意:实现时一定要恢复当前文件指针GetLength()。)
如果您需要使用选项 2,以下扩展名CStdioFile可用作直接替代,支持大于 2GB 的文件。
CStdioFileExt.h:
#pragma once
#include <afx.h>
class CStdioFileExt : public CStdioFile
{
DECLARE_DYNAMIC(CStdioFileExt)
public:
ULONGLONG GetPosition() const override;
ULONGLONG GetLength() const override;
ULONGLONG Seek(LONGLONG lOff, UINT nFrom) override;
};
Run Code Online (Sandbox Code Playgroud)
CStdioFileExt.cpp:
#include "CStdioFileExt.h"
ULONGLONG CStdioFileExt::GetPosition() const
{
ASSERT_VALID(this);
ASSERT(m_pStream != NULL);
auto const pos = _ftelli64(m_pStream);
if (pos == -1L)
AfxThrowFileException(CFileException::invalidFile, _doserrno, m_strFileName);
return static_cast<ULONGLONG>(pos);
}
ULONGLONG CStdioFileExt::GetLength() const
{
ASSERT_VALID(this);
auto const nCurrent = _ftelli64(m_pStream);
if (nCurrent == -1L)
AfxThrowFileException(CFileException::invalidFile, _doserrno, m_strFileName);
auto nResult = _fseeki64(m_pStream, 0, SEEK_END);
if (nResult != 0)
AfxThrowFileException(CFileException::badSeek, _doserrno, m_strFileName);
auto const nLength = _ftelli64(m_pStream);
if (nLength == -1L)
AfxThrowFileException(CFileException::invalidFile, _doserrno, m_strFileName);
nResult = _fseeki64(m_pStream, nCurrent, SEEK_SET);
if (nResult != 0)
AfxThrowFileException(CFileException::badSeek, _doserrno, m_strFileName);
return static_cast<ULONGLONG>(nLength);
}
ULONGLONG CStdioFileExt::Seek(LONGLONG lOff, UINT nFrom)
{
ASSERT_VALID(this);
ASSERT(nFrom == begin || nFrom == end || nFrom == current);
ASSERT(m_pStream != NULL);
if (_fseeki64(m_pStream, lOff, nFrom) != 0)
AfxThrowFileException(CFileException::badSeek, _doserrno, m_strFileName);
auto const pos = _ftelli64(m_pStream);
return static_cast<ULONGLONG>(pos);
}
IMPLEMENT_DYNAMIC(CStdioFileExt, CStdioFile)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
231 次 |
| 最近记录: |