Creating a named persistent stream

The preferred model for accessing file data in the CommonPoint application system is to use named, persistent streams. You can use file streams with either CommonPoint application system data or legacy data; however, using file streams with legacy data is likely to require very complex parsing.

To open an existing file for read or to write a new file out, you need to instantiate a TFileStream.

The TFileStream constructor takes three parameters:

TFileStream derives from TRandomAccessStream, and inherits significant streaming protocol. To learn about TRandomAccessStream, refer to Chapter 4, "Communications access."

TFileStream instances keep track of the end of file, also called end of stream. As you write out a file stream, the end-of-file marker moves forward. If you want to increase your efficiency, you can use TFileStream:SetLogicalEndOfStream to move the marker forward to the position you want.

NOTE When you move the end-of-file marker forward, the results you get depend on the host file system. Some file systems use more disk space to extend the file to match the new logical end-of-file marker. Other file systems do not change the end-of-file position until you write the file out. Still other file systems only allow you to allocate a portion of disk space as you use it, regardless of the logical end-of-stream location. Because your code might be used on different file systems, you need to code as though you risk running out of file space each time you move the end-of-file marker.

This example:

      TStandardText fileName("MyFile");
      TFile theFile = homeDirectory.CreateFile(fileName);
      TFileStream theFileStream(theFile);
      fileContents >>= theFileStream;
      FileSystemEntitySize length = theFileStream.GetEndOfFile();
      int amount = length.GetLeastSignificant();
      qprintf("Streaming out completed. File length: %d\n", amount);
This example opens an existing file and streams in the contents.

      TFileStream aFileStream(theFile);
      fileContents <<= aFileStream;

[Contents] [Previous] [Next]
Click the icon to mail questions or corrections about this material to Taligent personnel.
Copyright©1995 Taligent,Inc. All rights reserved.

Generated with WebMaker