Using TAutoClose

Example code showing how TAutoClose can be used to clean up when an object goes out of scope.

A simple example shows how this class can be used.

TInt ReadFile(const TDesC& aFile)
//
// Read a file
//
    {
    TAutoClose<RFile> file;
    TInt r=file.iObj.Open(aFile,KFileStreamText|EFileExclusive);
    if (r!=KErrNone)
        return(r);
    TBuf<100> b;
    if ((r=file.iObj.Read(b))!=KErrNone)
        return(r);
    ...
    }

The object file goes out of scope when the function completes, and the compiler automatically destroys it, calling the TAutoClose destructor in the process. The destructor calls Close() on the RFile member of TAutoClose.