Click to See Complete Forum and Search --> : CFile Example


Troy T
May 4th, 1999, 05:03 PM
I would like an example of how to structure the TRY and CATCH blocks for reading or writing a CFile. I have made this work before, but it wouldn't handle exceptions, so if the file didn't exist, it crashed. If the file was read-only, it crashed. I need to know how to do this, and the MSDN examples are not all inclusive to handle this situation.

Can someone give me just a brief code example of handling the exceptions (the standard ones that CFile uses)? Thank you very much in advance..

- Troy

suganyas
May 4th, 1999, 11:29 PM
Hi!
This is how CFile Exceptions are handled.

Here is the sample code to handle exceptions which you have specified. The m_cause takes values depending on the cause of failure. Refer help for the same.

In case U try to open an file that does not exist.
CFileException e;
char* pFileName = "ReadU.txt";
if( !f.Open( pFileName,CFile::modeWrite, &e ) )
{
if(e.m_cause==CFileException::fileNotFound)
AfxMessageBox("File Not found");
}
Do let me know if this works for U.

Regards,
Sukanya Swaminathan
Software Engineer
Aditi Technologies Pvt. Ltd
Blr -80

Troy T
May 5th, 1999, 12:07 AM
Thanks, that was exactly what I needed. I was going about that the wrong way, and you enlightened me on the correct process. Thank you very much! :)

- Troy