Understanding the Header Files - C++

In the preceding section’s bullet on stream objects, we listed several header files. Each of these header files encapsulates a section of the overall C++ stream architecture. All these files come together with each other to give the complete I/O support provided by C++. Each of these files completes its specific task to give C++ the most diverse I/O functionality of any programming language. The purpose of each of the headers is as follows:

  • <fstream>. Contains the definition for several template classes that support the iostream header file’s operations on sequences stored in external files.
  • <iomanip>. Contains several single-argument manipulators.
  • <ios>. Contains a majority of the format manipulators basic to the operation of iostream header file’s classes.
  • <iosfwd>. Contains the forward declarations for the iostream header file’s classes.
  • <iostream>. Contains the declarations for the standard global stream objects such as cin and cout.
  • <istream>. Contains the extractors for inputting data from streams and includes the template class basic_istream. In other words, <istream> puts the I in I/O.
  • <ostream>. Contains the inserters for outputting a series of bytes and includes the template class basic_ostream. Basically, <ostream> puts the O in I/O.
  • <sstream>. Supports the iostream header file’s classes’ operations on several sequences stored in character arrays with several template classes.
  • <streambuf>. Defines the basic_streambuf template class.
  • <strstream>. Defines classes that support the iostream header file’s classes’ operations on sequences stored in C-style character arrays.

With the exception of the ios_base class, each template class has a specialization class for characters. Figure shows the most commonly used template classes and their character specialization classes. However, this is not a complete list because several of the stream classes are beyond the scope of this book.

The class contained within each header file and the classes from which they are derived.Terms in bold represent header files, and terms without bold represent classes. The lines that link the classes indicate from which classes each class is inherited.

class contained within each header file and the classes

The ios_base Class

The ios_base class contains the byte storage common to all streams and the methods that are not dependent on template parameters. This byte storage stores the bytes that are being input and output until they reach their final destination. The several data types contained within the ios_base class provide the basis for controlling streams.

The following list outlines the purpose of several data types that can be used to control streams:

  • event. Use the event information in the data type event to store callbacks. Callbacks are pointers to methods that are registered with and called by the operating system when a specified significant event happens.
  • fmtflags. Use format flags in the data type fmtflags to specify information about the format of the stream.
  • iostate. Use I/O state information in the data type iostate to keep track of the current state of I/O operations. Use the iostate data type to check for stream corruption.
  • openmode. Use open mode to specify a stream’s type of read/write
  • access.
  • seekdir. Use seek direction to specify where the stream data should be sought and store it in the type alias seekdir.

In addition, you can use several methods provided by the ios_base and in its derived classes (all other stream classes). Here they are in alphabetical order:

  • flags(). Sets or returns format flag information.
  • getloc(). Returns an internal local object that encapsulates the stream’s information.
  • imbue(). Stores a local object that encapsulates the stream’s information and returns its previously stored object.
  • ios_bas(). This is the constructor.
  • iword(). Returns a reference to a specified element of the extensible array having elements of type long.
  • operator =() (overloaded assignment operator). Copies everything in the object to a new stream object including formatting information and extensible arrays.
  • precision(). Determines the number of decimal places in the display’s precision.
  • pword(). Returns a reference to a specific element of the void* array.
  • register_callback(). Stores callback information for stream callback events.
  • setf(). Sets new flags for the stream and returns the previously set flags.
  • unsetf(). Clears all flags.
  • width(). Sets or retrieves the field width of a stream.
  • xalloc(). Returns a unique static variable to be used in iword() or pword() as an array index.

Much more could be written about the ios_base class. However, to keep this book interesting and within its intended page count, we decided to focus on applying streams to their most common uses.


All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

C++ Topics