CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: CFile Example

  1. #1
    Join Date
    May 1999
    Location
    WA
    Posts
    65

    CFile Example

    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

  2. #2
    Join Date
    May 1999
    Posts
    29

    Re: CFile Example

    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



  3. #3
    Join Date
    May 1999
    Location
    WA
    Posts
    65

    Re: CFile Example

    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured