Commit calls the OutStream function to open an output stream. This
function is defined in TFileDocument and returns a TOutStream *.
Commit then writes the data to the output stream. The procedure for
this is almost exactly identical to that used in the old SaveFile function.
After writing the data to the output stream, Commit turns the IsDirty
flag off by calling SetDirty with a FALSE parameter It then returns.
TRUE, indicating that the operation was successful.
Here’ how the code for your Commit function might look:s
BOOL TDrawDocument::Commit(BOOL force)
{
if (!IsDirty() && !force)
return TRUE;
TOutStream* os = OutStream(ofWrite);
if (!os)
return FALSE;
// Write the number of lines in the figure
*os << Lines->GetItemsInContainer();
// Append a description using a resource string
*os << ’ ’ << string(*GetDocManager().GetApplication(),IDS_FILEINFO) <<
’\n’;
// Get an iterator for the array of lines
TLinesIterator i(*Lines);
// While the iterator is valid (i.e. you haven’t run out of lines)
while (i) {
// Copy the current line from the iterator and increment the array.
*os << i++;
}
delete os;
SetDirty(FALSE);
return TRUE;
}
There’ only one thing in the Commit function that you haven’t seens
before:
// Append a description using a resource string
*os << ’ ’ << string(*GetDocManager().GetApplication(), IDS_FILEINFO) <<
’\n’;
This uses a special constructor for the ANSI string class:
string(HINSTANCE instance, UINT id, int len = 255);
Chapter 2, Learning ObjectWindows
81