FIX: CFile::ReadHuge May Return Incorrect Value

Last reviewed: September 18, 1997
Article ID: Q109849
1.00 WINDOWS kbprg kbfixlist kbbuglist

The information in this article applies to:

  • The Microsoft Foundation Classes (MFC), included with:

        - Microsoft Visual C++ for Windows, version 1.0
    

SYMPTOMS

The member function CFile::ReadHuge may return an incorrect value.

CAUSE

A miscalculation exists in the CFile::ReadHuge() function located in FILEX.CPP. FILEX.CPP may be found in the \MSVC\MFC\SRC directory.

RESOLUTION

To correct the problem, replace the code for CFile::ReadHuge() with the code below. You can modify the Microsoft Foundation Class (MFC) Library sources or you could make the function a new function of some CFile-derived class.

Sample Code

DWORD CFile::ReadHuge(void FAR* lpBuffer, DWORD dwCount) {

     ASSERT_VALID(this);

     DWORD dwToRead = dwCount;
     while (dwToRead > 0)
     {
          UINT nRead = _AfxCalcSize(dwToRead, lpBuffer);
          UINT nActuallyRead;
          if ((nActuallyRead = Read(lpBuffer, nRead)) < nRead)
               return ((dwCount - dwToRead) + nActuallyRead);
          ASSERT(nActuallyRead == nRead);
          dwToRead -= nRead;
          lpBuffer = ((BYTE _huge*)lpBuffer) + nRead;
     }
     return dwCount;
}

STATUS

Microsoft has confirmed this to be a bug in the products listed at the beginning of this article. This problem was corrected in MFC 2.5, included with Visual C++ for Windows, version 1.5.

MORE INFORMATION

The code below illustrates the problem. When the program is executed, the output line will be:

   Read returns 0, ReadHuge returns 512

Both reads should return zero for a zero-length file.

Sample Code

   // compile options required: none

   void Test()
   {
      CFile   file;
      CFileException exception;
      char *   buffer[1024];
      UINT    read1;
      DWORD   read2;

      ASSERT(file.Open( "blank.txt",
                 CFile::modeReadWrite
               | CFile::typeBinary
               | CFile::modeCreate,
                &exception ));

      read1 = file.Read( buffer, 512 );

      file.Seek( 0, CFile::begin );
      read2 = file.ReadHuge( buffer, 512 );

      TRACE( "Read returns %d, ReadHuge returns %ld\n",
               read1, read2 );

      file.Close();
   }


Additional reference words: 1.00 2.00
KBCategory: kbprg kbfixlist kbbuglist
KBSubcategory: MfcFileIO
Keywords : kb16bitonly MfcFileIO kbbuglist kbfixlist kbprg
Technology : kbMfc
Version : 1.00
Platform : WINDOWS
Solution Type : kbfix


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: September 18, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.