CFile myFile("example.txt", CFile::modeRead); // do some file operations... myFile.Close();
CFile myFile("nonexistent.txt", CFile::modeRead); if (!myFile.m_hFile) { // file could not be opened, handle error... } else { // do some file operations... myFile.Close(); }This example shows how to handle errors that may occur when attempting to open a file. If the file cannot be opened (i.e. `myFile` has a null `m_hFile` member), we can handle the error appropriately. Otherwise, we perform some file operations and then close the file with the `Close` function. Package Library: Microsoft Foundation Class (MFC) library.