最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

c++ - Unexpected AfxThrowFileException in CStdioFile::Read() - Stack Overflow

programmeradmin0浏览0评论

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

filetxt.cpp in function UINT CStdioFile::Read
if ((nRead = (UINT)fread(lpBuf, sizeof(BYTE), nCount, m_pStream)) == 0 && !feof(m_pStream))
   AfxThrowFileException(CFileException::genericException, _doserrno, m_strFileName);
Questions:
  • 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?

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.

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

filetxt.cpp in function UINT CStdioFile::Read
if ((nRead = (UINT)fread(lpBuf, sizeof(BYTE), nCount, m_pStream)) == 0 && !feof(m_pStream))
   AfxThrowFileException(CFileException::genericException, _doserrno, m_strFileName);
Questions:
  • 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?

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.

Share Improve this question edited Feb 4 at 11:20 Jabberwocky 50.9k18 gold badges68 silver badges124 bronze badges asked Feb 4 at 10:41 A AkshathaA Akshatha 213 bronze badges 7
  • What's the value of _doserrno? Should be included in the exception report. – Richard Critten Commented Feb 4 at 10:47
  • 1 Question, why don't you use C++ standard file streams and exceptions? You can and really should write most of your code in standard C++. There is no reason to use MFC for things other than building the UI part (and the Windows API for things like registry access). – Pepijn Kramer Commented Feb 4 at 10:47
  • OK, I got. You are asking about MFC source code. You should have mentioned this in the question. – Jabberwocky Commented Feb 4 at 10:59
  • 1 @PepijnKramer The MFC framework is so old that it can't cope with std::exception. That's enough reason to stick with what's there. – j6t Commented Feb 4 at 11:21
  • @molbdnilo Demonstration (without error checks for brevity). – Jabberwocky Commented Feb 4 at 11:34
 |  Show 2 more comments

1 Answer 1

Reset to default 0
Q:

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

A:

Most likely disk I/O errors, failure to read from a mistreated CDRom, damaged hard drive, network errors, removing an USB stick while you're reading from it and so on.


Q:

Could this be due to file permissions, locking, or disk errors?

A:

See answer above. Most likely not from file permissions, because you wouldn't have been able to open file file in the first place. Locking? Unlikely, but I'm not quite sure here.


Q:

Are there any specific scenarios where fread fails but feof remains false?

A:

IMO only errors, see questions above.


Q:

How should I handle this exception properly to differentiate between different failure scenarios?

A:

It depends on your use case. But most likely not. You read from a file, you get an error, well then handle it, for example display an error message.

发布评论

评论列表(0)

  1. 暂无评论