Your browser doesn't support JavaScript MFC Data Handling – Windows Programming

MFC Data Handling

The Microsoft Foundation Class (MFC) library provides a comprehensive collection of classes for storing, manipulating and exchanging data. These classes simplify many common programming tasks by encapsulating frequently used data structures and Windows data types within easy-to-use C++ classes.

MFC data handling facilities fall into three broad categories:

  • Collection classes, which manage groups of related objects.
  • Simple data type classes, which encapsulate commonly used Windows data types.
  • File classes, which provide object-oriented access to files and data storage.

Together these classes allow applications to manage data efficiently while reducing the amount of code required by the programmer.


MFC Collection Classes

Collection classes are designed to manage groups of related objects dynamically. Unlike ordinary C++ arrays, most MFC collection classes automatically increase or decrease in size as elements are added or removed. They also provide convenient member functions for inserting, deleting, searching and retrieving objects.

MFC collection classes fall into two categories:

  • Template collection classes
  • Non-template collection classes

Template collection classes provide compile-time type checking and are generally recommended for modern MFC development. Non-template collection classes remain available primarily for compatibility with existing applications developed before C++ templates became widely supported.

Template Collection Classes

Template collection classes allow programmers to store objects of almost any data type while maintaining compile-time type safety. Because the compiler knows the type of object stored within the collection, many programming errors can be detected during compilation rather than at run time.

The principal template collection classes supplied with MFC are described below.

ClassDescription
CArrayImplements a dynamically sized array that supports random access to its elements. The array automatically expands or contracts as elements are added or removed.
CListImplements a doubly linked list that allows efficient insertion and removal of elements at any position within the collection.
CMapImplements an associative array (dictionary) that maps unique keys to corresponding values, allowing rapid retrieval of stored objects.
CTypedPtrArrayProvides a type-safe wrapper around pointer array collection classes.
CTypedPtrListProvides a type-safe wrapper around pointer list collection classes.
CTypedPtrMapProvides a type-safe wrapper around pointer map collection classes.

Template collection classes are suitable for most new MFC applications because they combine flexibility with compile-time type checking, reducing the possibility of programming errors.

Non-Template Collection Classes

Before C++ templates became widely available, MFC provided a number of collection classes designed to store specific data types. Although these classes remain fully supported for compatibility with older applications, they have largely been superseded by the template collection classes.

The principal non-template collection classes are listed below.

ClassDescription
CObArrayDynamic array of pointers to objects derived from CObject.
CByteArrayDynamic array of BYTE values.
CDWordArrayDynamic array of DWORD values.
CPtrArrayDynamic array of generic pointers (void*).
CStringArrayDynamic array of CString objects.
CUIntArrayDynamic array of unsigned integer values.
CWordArrayDynamic array of 16-bit word values.
CObListDoubly linked list of pointers to objects derived from CObject.
CPtrListDoubly linked list of generic pointers (void*).
CStringListDoubly linked list of CString objects.
CMapPtrToPtrMaps pointer keys to pointer values.
CMapPtrToWordMaps pointer keys to 16-bit word values.
CMapStringToObMaps CString keys to pointers to CObject-derived objects.
CMapStringToPtrMaps CString keys to generic pointers.
CMapStringToStringMaps CString keys to CString values.
CMapWordToObMaps 16-bit word keys to pointers to CObject-derived objects.
CMapWordToPtrMaps 16-bit word keys to generic pointers.

Although non-template collection classes continue to be supported, programmers developing new MFC applications will generally find the template collection classes more flexible and easier to use.

Simple Data Type Classes

In addition to its collection classes, MFC provides a number of utility classes that encapsulate commonly used Windows data types. These classes simplify many programming tasks by providing convenient constructors, operators and member functions for manipulating strings, dates, times, coordinates, rectangles and other frequently used data.

Many of these classes are used extensively throughout the MFC library and appear as parameters or return values in numerous Windows and MFC member functions.

The principal simple data type classes are described below.

ClassDescription
CStringEncapsulates character strings and provides a comprehensive set of member functions for string manipulation, searching, formatting and comparison.
CTimeRepresents an absolute date and time value and provides functions for formatting and performing date and time calculations.
CTimeSpanRepresents a period of elapsed time rather than a specific date or time.
COleDateTimeEncapsulates the OLE Automation DATE data type and is commonly used by COM and ActiveX applications.
COleDateTimeSpanRepresents a time interval associated with COleDateTime objects.
CPointRepresents a two-dimensional point specified by x and y coordinates.
CSizeRepresents the width and height of an object or a horizontal and vertical displacement.
CRectRepresents a rectangular region using the coordinates of its upper-left and lower-right corners.
CImageListEncapsulates a Windows image list containing one or more small bitmap or icon images used by common controls.
COleVariantEncapsulates the OLE Automation VARIANT data type capable of storing values of many different types.
COleCurrencyEncapsulates the OLE Automation CURRENCY data type used for fixed-point monetary values.

These utility classes eliminate much of the complexity associated with manipulating the corresponding Windows data structures directly and provide a consistent object-oriented programming interface throughout the MFC library.


File Input and Output

Almost every Windows application requires the ability to store and retrieve information from files. Rather than requiring programmers to use the low-level Windows file handling functions directly, MFC provides a comprehensive set of classes that encapsulate file operations within an object-oriented framework.

The principal file handling class is CFile, which provides member functions for opening, creating, reading, writing, seeking and closing files. In addition, MFC provides derived classes that extend the capabilities of CFile for specialised applications.

The most commonly used file classes are listed below.

ClassDescription
CFileProvides general-purpose binary file input and output operations.
CStdioFileDerived from CFile; provides text file operations using the C run-time library.
CMemFileImplements a file whose contents are stored entirely in memory rather than on disk.
CSharedFileExtends CMemFile by allowing the memory buffer to be shared between applications, particularly during clipboard and OLE operations.

The CFile class supports numerous operations including:

  • Creating new files.
  • Opening existing files.
  • Reading data from a file.
  • Writing data to a file.
  • Seeking to a specified location within a file.
  • Determining the current file position.
  • Retrieving the file length.
  • Renaming files.
  • Deleting files.
  • Closing files.

Files are typically opened by calling the Open() member function, which specifies the filename together with the required access mode, such as read-only, write-only or read/write access. Once the file has been opened successfully, data may be transferred using the Read() and Write() member functions.

When all file operations have been completed, the application should call the Close() member function to release the associated system resources.


Choosing the Appropriate Collection Class

The large number of available collection classes may initially appear confusing. Fortunately, selecting the appropriate collection is usually straightforward.

  • Use CArray when random access by index is required.
  • Use CList when frequent insertion or deletion of elements is required.
  • Use CMap when objects must be retrieved quickly using a unique key.
  • Use CStringArray or CStringList when storing collections of text strings.
  • Use one of the pointer collection classes when storing pointers to dynamically allocated objects.

In general, template collection classes should be preferred for new applications because they provide better type checking and greater flexibility. The older non-template collection classes remain valuable when maintaining legacy applications or when compatibility with existing MFC code is required.

Summary

The MFC library provides a comprehensive set of classes for managing data within Windows applications. Collection classes simplify the storage and manipulation of groups of objects, utility classes encapsulate many of the Windows data types commonly encountered during programming, and the file classes provide a convenient object-oriented interface for persistent storage.

Although modern C++ applications frequently make use of the Standard Template Library (STL), the MFC collection and utility classes remain an important part of the framework and continue to be widely used in existing Windows applications. A sound understanding of these classes enables programmers to develop efficient, maintainable applications while taking full advantage of the facilities provided by the Microsoft Foundation Class Library.