Programming Windows with MFC, Second Edition

[Previous] [Next]

Chapter 6

File input and output (I/O) services are a staple of any operating system. Not surprisingly, Microsoft Windows provides an assortment of API functions for reading, writing, and manipulating disk files. MFC casts these functions in an object-oriented mold with its CFile class, which lets files be viewed as objects that are operated on with CFile member functions such as Read and Write. CFile has all the tools the MFC programmer needs to perform low-level file I/O.

The most common reason for writing file I/O code is to support document saving and loading. Although there's nothing wrong with using CFile objects to write documents to disk and read them back, most MFC applications don't do it that way; they use CArchive objects instead. Thanks to some strategic operator overloading performed by MFC, most data can be serialized—that is, output as a byte stream—to a CArchive or deserialized from a CArchive with syntactical ease. Moreover, if a CArchive object is attached to a CFile object, data that is serialized to the CArchive is transparently written to disk. You can later reconstitute data archived in this manner by deserializing it from a CArchive associated with the same file.

The ability to save and load documents by serializing them to or from a CArchive is one of the fundamental building blocks of MFC's document/view architecture. Although knowledge of CArchive is of limited use for now, rest assured that it will come in exceedingly handy when we begin writing document/view applications in Chapter 9.

Категории