簡體   English   中英

CStdioFile 不能處理大於 2GB 的文件?

[英]CStdioFile cannot work with files larger than 2GB?

我正在使用 Visual C++ 2008。在 VC++ 2008 中, CFile支持 2^64 個大文件。 所以我認為CStdioFile也應該支持。

但是,當在大於 2GB 的文件上使用CStdioFile::GetLength()時,我得到一個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();
    }
}

跟蹤到CStdio::GetLength()后,我發現以下代碼段會引發異常,如下所示:

   nCurrent = ftell(m_pStream);            -> This will return -1
   if (nCurrent == -1)
      AfxThrowFileException(CFileException::invalidFile, _doserrno,
         m_strFileName);

令人驚訝的是, CStdioFile仍然使用ftell而不是_ftelli64來處理 stream。

然后我在文檔中搜索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 ,但它沒有提供解決方案。

除此之外,網上幾乎沒有任何關於CStdioFile 2GB 限制的問題或帖子。 這真的很奇怪。

我嘗試檢查CStdioFile iN VC++ 2017 的源代碼,它與 2008 的源代碼相同。

那么在不重寫整個CStdioFile class的情況下是否有簡單的解決方案呢?

CStdioFile的存在主要有一個原因:它的構造函數采用FILE*參數。 此 class 的目的是包裝 C 運行時文件並通過CFile兼容接口公開它。 該實現繼承了所有 C 運行時限制,特別是 2GB 的文件大小限制。

為了解決這個問題,有幾種選擇:

  1. 如果可能,完全放棄使用CStdioFile 除非您與(舊版)C 代碼交互,否則沒有明顯的理由使用它。

  2. 如果這不是一個選項,請從CStdioFile派生自定義實現並override所有表面文件相對偏移量的 class 成員( GetPosition()GetLength()Seek() )。 所有其他 class 成員不受影響,可以簡單地繼承。 (注意:確保在實現GetLength()時恢復當前文件指針。)

    Microsoft 為其 C 運行時提供擴展,提供 64 位寬偏移( _ftelli64_fseeki64 )。

如果您需要 go 與選項 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;
};

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)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM