Using stream classes

You can print floating point numbers and integers to the console using the cout object (an instance of the ostream class) or to files with an instance of ofstream. Both of these classes will convert numbers to strings using member methods and manipulators to affect the formatting of the output string. Similarly, the cin object (an instance of the istream class) and the ifstream class can read data from formatted streams.

Manipulators are functions that take a reference to a stream object and return that reference. The Standard Library has various global insertion operators whose parameters are a reference to a stream object and a function pointer. The appropriate insertion operator will call the function pointer with the stream object as its parameter. This means that the manipulator will have access to, and can manipulate, the stream it is inserted into. For input streams, there are also extraction operators that have a function parameter which will call the function with the stream object.

The architecture of C++ streams means that there is a buffer between the stream interface that you call in your code and the low-level infrastructure that obtains the data. The C++ Standard Library provides stream classes that have string objects as the buffer. For an output stream, you access the string after items have been inserted in the stream, which means that the string will contain those items formatted according to those insertion operators. Similarly, you can provide a string with formatted data as the buffer for an input stream, and when you use extraction operators to extract the data from the stream you are actually parsing the string and converting parts of the string to numbers.

In addition, stream classes have a locale object and stream objects will call the conversion facet of this locale to convert character sequences from one encoding to another.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset