Files and TFile

A TFile instance is a surrogate entity that maps to a physical file. You create files using TDirectory::CreateFile and access their properties using the TFile. Each of the file access classes--TFileStream and TMappedFile--take a TFile instance as a parameter. You can delete the physical file entity by calling aFile::DeleteSelf.

Getting a file or directory instance

Before you can create a new file, you need a TDirectory instance that corresponds to the physical location where you want to store the file. Before you can access properties of a file or open a file stream or mapped file, you need to obtain the TFile instance that maps to the file you want to use. Many times another program or the workspace hands the file system entity directly to your program, but this might not always be the case. Some alternate methods of getting a file system entity are:

See "Obtaining a file system entity from a pathname" on page 49 for an example of how to use TPathName to get the entity.

To find a file system entity based on a set of characteristics, iterate through a volume using a property query. Construct a property query that specifies the characteristics of the file system entity you want, and then pass the query into a TDirectoryTreeIterator or TDirectory::LookUp. Read "Iterating through directories and volumes" on page 64 for examples that use TDirectoryTreeIterator.

NOTE An iterator returns any directory that meets the characteristics you specified in your query, while LookUp returns only the first match to your query.

If you need a TDirectory instance and do not have a TVolume instance through which you can iterate or perform a lookup, you need to obtain the TVolume instance first. You can get a TVolume either by having an external program pass you the instance, or by using TVolumesIterator to find the one you want.

This example obtains a TDirectory instance using TPathName and creates a file within the directory that TDirectory represents. This code assumes that myPath and myFileName were previously defined.

      TPathName thePath(myPath);
      TDirectory myDirectory = thePath.GetEntity();
      TFile myFile = myDirectory.CreateFile(myFileName);

Deleting a file

You delete a file by calling
TFile::DeleteSelf (inherited from TFileSystemEntity).

CAUTION DeleteSelf deletes the physical file from the device.


[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