How to store and restore text

Storing text and its formatting

theFs represents a session with the file server.

RFs theFs;
CFileStore* theStore;
TParse fileStoreName;
TBufC<28> name(_L("C:\\Documents\\richtxt.dat"));
TStreamId streamId;

A session with the file server must be opened before file-related operations are carried out. Use RFs::Connect() to open a session with the file server.

Call StoreL() on the CRichText object to externalise the text object to a CFileStore. The stream in which the text is stored is identified by its ID. This will later be used to restore the rich text.

Call Close() on the RFs object to close the file as well as the file server session.

// Store text to a file store
theFs.Connect();
theFs.Parse(name,fileStoreName);
theStore=CDirectFileStore::ReplaceLC
    (theFs,fileStoreName.FullName(),EFileRead|EFileWrite);
theStore->SetTypeL(KDirectFileStoreLayout);
// externalize the rich text
streamId=iRichText->StoreL(*theStore); // Store and get ID
CleanupStack::PopAndDestroy(); // pop and destroy store
theFs.Close();

Restoring rich text

The following example restores the text object by calling RestoreL() on the CRichText object, specifying the file store and the ID of the stream in which the rich text is stored.

The file is opened for reading only.

// Restore text from file store
theFs.Connect();
theFs.Parse(name,fileStoreName);theStore=CDirectFileStore::OpenLC
    (theFs,fileStoreName.FullName(),EFileRead|EFileShareReadersOnly);
if (theStore->Type()[0]!=KDirectFileStoreLayout)
    User::Leave(KErrUnknown);
 // internalize from the store
RichText->RestoreL(*theStore,streamId);
CleanupStack::PopAndDestroy(); // pop and destroy store
theFs.Close();