I am using Visual C++ 2008. In VC++ 2008, CFile
supports 2^64 huge files. So I think CStdioFile
should also support.
However, when using CStdioFile::GetLength()
on a file larger than 2GB, I get a CFileException
, below is the code snippet:
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();
}
}
After tracing into the CStdio::GetLength()
, I find the following code snippet will raise exception, as below:
nCurrent = ftell(m_pStream); -> This will return -1
if (nCurrent == -1)
AfxThrowFileException(CFileException::invalidFile, _doserrno,
m_strFileName);
It is amazing that CStdioFile
still use ftell
instead of _ftelli64
to deal with the stream.
I then search the document for CStdioFile
, I cannot find any document on CStdioFile::GetLength
, the only related is https://learn.microsoft.com/en-us/cpp/mfc/reference/cstdiofile-class?view=vs-2019#seek, and it asks me to see fseek
document. But in fseek
document, I still not find any related to file size limit.
Finally I find a third-party site that indicates CStdioFile::GetLength
contains error: http://www.flounder.com/msdn_documentation_errors_and_omissions.htm#CStdioFile::GetLength, but it does not provide a solution.
Other than these, there are hardly any questions or posts on the 2GB limit of CStdioFile
online. That is really strange.
I try to check the source code of CStdioFile
iN VC++ 2017 and it is same as that for 2008.
So whether there is a simple solution for the problem without rewriting the whole CStdioFile
class?