Unexpected AfxThrowFileException in CStdioFile::Read()

Praveena S Kumar 20 Reputation points
2025-02-03T10:55:36.34+00:00

I am using CStdioFile::Read in an MFC application, and I noticed that an exception is thrown in the following condition:

if ((nRead = (UINT)fread(lpBuf, sizeof(BYTE), nCount, m_pStream)) == 0 && !feof(m_pStream))         AfxThrowFileException(CFileException::genericException, _doserrno, m_strFileName);

I understand that nRead == 0 means no bytes were read, but since feof(m_pStream) is false, it suggests that EOF has not been reached.

Also, before calling CStdioFile::Read, we have checked that the file is open using CStdioFile::Open function too.

Below is the sample code :

static bool ReadIniFile(
    const CString& cstrIniFilePath,   
    std::map<CString, CString>& IniMap
)
{
    bool bRet = false;
    CStdioFile cFile;
    UINT TextMode = CFile::modeRead | CFile::typeText | CFile::shareDenyNone;
 
    IniMap.clear();
 
    if (cFile.Open(cstrIniFilePath, CFile::modeRead | CFile::typeBinary | CFile::shareDenyNone))   
    {
        BYTE cBuf[16];
        if ((cFile.Read(cBuf, 2) == 2) && (cBuf[0] == 0xFF) && (cBuf[1] == 0xFE)) // Crash point
        {
            TextMode = CFile::modeRead | CFile::typeUnicode | CFile::shareDenyNone;
        }
        cFile.Close();
 
        if(cFile.Open(cstrIniFilePath, TextMode))
        {
            if ((TextMode & CFile::typeUnicode) != 0)
            {
                cFile.Seek(2, CFile::begin);
            }
 
            CString strLine;
            CString strSection;
            CString strKey;
            CString strValue;
            while(cFile.ReadString(strLine))
            {
                strLine.Trim();
                int len = strLine.GetLength();
                if(len > 2)
                {
                    if(strLine[0] == '[')
                    {
                        int n = strLine.Find(']');
                        if(n >= 2)
                        {
                            strSection = strLine.Mid(1, (n - 1));
                        }
                    }
                    else if(!strSection.IsEmpty())
                    {
                        int n = strLine.Find('=');
                        if((n > 0) && ((n + 1) < len))
                        {
                            strKey = strLine.Left(n);
                            strKey.TrimRight();
                            strValue = strLine.Mid(n + 1, len - n);
                            strValue.TrimLeft();
 
                            CString strMapKey = strSection + _T("&") + strKey;
                            strMapKey.MakeUpper();
                            strValue.Replace(_T("\\n"), _T("\n"));
                            auto itr = IniMap.find(strMapKey);
                            if(itr == IniMap.end())
                            {
                                IniMap[strMapKey] = strValue;
                            }
                        }
                    }
                }
            }
            cFile.Close();
            bRet = true;
        }
    }
    return bRet;
}

Here file seems valid and not corrupted.

Question:

What are the possible reasons for this exception to be triggered?

  • Could this be due to file permissions, locking, or disk errors?
  • Are there any specific scenarios where fread fails but feof remains false?
  • How should I handle this exception properly to differentiate between different failure scenarios?
C++
C++
A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.
3,879 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Ratheesh V A 0 Reputation points
    2025-03-07T09:18:11.0733333+00:00

    @RLWA32 , @David Lowndes Can you please check is this informative for issue scenario.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.