How To Test for End of File with CFile and CStdioFile (88769)



The information in this article applies to:

  • The Microsoft Foundation Classes (MFC), when used with:
    • Microsoft Visual C++ for Windows, 16-bit edition 1.0
    • Microsoft Visual C++ for Windows, 16-bit edition 1.5
    • Microsoft Visual C++ for Windows, 16-bit edition 1.51
    • Microsoft Visual C++ for Windows, 16-bit edition 1.52
    • Microsoft Visual C++, 32-bit Editions 1.0
    • Microsoft Visual C++, 32-bit Editions 2.0
    • Microsoft Visual C++, 32-bit Editions 2.1
    • Microsoft Visual C++, 32-bit Editions 2.2
    • Microsoft Visual C++, 32-bit Editions 4.0

This article was previously published under Q88769

SUMMARY

The Microsoft Foundation Classes (MFC) provide a CFileException class. The CFileException class includes public data members that contain operating- system errors and C Run-time errors that may be generated by certain file operations.

MORE INFORMATION

The CFile and CStdioFile classes may throw a CFileException when certain error conditions occur; however, these classes do not throw a CFileException when the end of file is reached. If an application using the CFile or CStdioFile class needs to check for the end-of-file condition when reading from a file, the return values from the Read() and ReadString() member functions should be checked.

CFile::Read returns the actual number of bytes read. If this number is less than the bytes requested, then the end of file has been reached.

CStdioFile::ReadString returns a NULL pointer when the end of file has been reached.

Modification Type:MinorLast Reviewed:7/13/2004
Keywords:kbFileIO kbhowto KB88769