This example opens an existing file for shared reading and streams in the contents. The second IFile::in in the constructor's argument list allows other processes to open the file for concurrent reading.
IFile theFile(pathName); IDataStream* stream = theFile.createStream(IFile::in, IFile::in); ISomeDataType* fileContents = NULL; ::readObject(fileContents, *stream); delete stream;
IFileIOStream allows you to access the data within a file and derives from the standard library class iostream, providing interoperability with C++ iostreams.
You can create an IFileIOStream using an IFile instance to specify the file whose contents you want to stream:
IFile aFile(pathName, IFile::kCreateIfNeeded); IFileIOStream aStream(aFile); aStream << "I'm writing this string to the stream." << endl;
You can also use a full or partial pathname that specifies the file:
IFileIOStream aStream(hostPathName, modeForMe, modeForOthers, IUnixPathNameParser());
IDataStream is the standard Open Class stream for C++ objects. To access a file's contents with an IDataStream object, call the file entity's createStream function. The createStream function returns a pointer to an IDataStream. You are responsible for deleting the pointer when you are finished with the stream:
IFile aFile(pathName); IDataStream* stream = aFile.createStream(IFile::in + IFile::out + IFile::trunc); ::writeObject(fileContents); cout << "streaming out completed." << endl; delete stream;
A file remains open until the stream's close function is called or until the IFileIOStream instance is destroyed. If you allocate one of these instances on the heap, you take responsibility for destroying the instance (and thus closing the file) when you are done.
Overview
of File System Entities
Overview
of Pathnames and Pathname Parsers
Data
Accessors
File System
Exceptions
Instantiating a
File System Entity from a Host-Specific Pathname
Instantiating
a File System Entity from a Portable Pathname
Instantiating
a File System Entity from Another Instance of the Same Entity
Class
Using an
Instance of IFileSystemEntity to Represent a File, Directory or
Volume
Creating and
Replacing Files and Directories