CFile Sample


Click here to change the theme.

The following is a sample of opening and reading a file using the MFC CFile class. See File-Handling Tasks in the MFC documentation for some descriptions of using CFile.

	char Buffer[100];
	CFile File;
	CFileException fe;
if (!File.Open(pszPathName, CFile::modeRead, &fe)) {
	fe->ReportError();
	return FALSE;
	}
TRY {
	while (File.Read(&Buffer, sizeof(Buffer))) {
		// Process data in Buffer
		}
	File.Close();
	}
CATCH_ALL(e) {
	File.Abort(); // will not throw an exception
	e->ReportError();
	return FALSE;
	}
END_CATCH_ALL