The foundation of every relationship is communication. The quality of the communication is the first factor that determines the way the relationship evolves. This statement also holds true in the world of C++, and to raise the quality of communication, a library called iostream has been implemented.
Iostream Library and Basic I/O in C++ - The Iostream Library (Page 2 of 4 )
Briefly sketched, the relations between the classes within the library look like this:
-> The picture is property of the IBM XL C/C++ V9.0 for AIX help <-
The upper classes are just type definitions for the char type. There exists also a wchar_t (wide/UNICODE) correspondent for each one, as all of this is written commonly in a basic_ side for each one, as you may see if you open up the iosfwd header:
This template build assures the stream functionality in a type-independent manner. Each one has two parameters: the type of the stream in the first and the additional character-dependent specifics in the second char_traits parameter.
To complete the upper picture we must explain that ios itself is derived from ios_base, that, on the other hand, is type-independent and therefore does not has a basic pair.
The narrow kind of chars are better known, but slowly the world should start moving on the wide char as mentioned in the Introduction to Streams article, for practice- and money-oriented reasons. These are used in the same manner as the narrow ones; it's just that the name/definition is started with the w char.
To increase portability inside the upstream classes, you will barely find any proper fundamental types in their member prototypes. Instead, they have members like the ones used for the stream position, offset; the defined size depends on the char traits (the second parameter in the template).
The manipulators are functions that take a parameter that will modify based on the property of the stream. With these you can use formats similar to the sprint in C++, for instance, or automatic conversion from the stream container upon reading, applied using the >> operator.
Now we need to look over the upper diagram and see what each one should do. For a start, there are the ios, istream, ostream, streambuf classes that are rarely used directly by most programmers. They represent the base for the rest, or do the work in the background and are automatically included inside the derived classes, so you do not need to be concerned with them.
Thefstreampart is responsible for the file input/output using the streams. While the fstream is able to handle both the Input and Output work, there exists theifstream/ofstreamthat allows data flow only in either the inward or outward direction. Using these, you can make sure that you will not mess up and do a task you should not -- like trying to read a value when you should print it.
If you've heard about STL before, you probably heard about the string type/class. If you did not, no problem; we shall talk about that also. For now it is enough to perceive them as an automatically managed array of chars (wide or narrow). Thesstream is the one responsible, so you can handle this just like a stream.
Then again, there is theiomanipclass, that contains already-defined manipulators. Though there are quite a few already defined, sometimes you may want to write a new one for yourself. This task will also be discussed in a future article.
Theiostreamclass finally contains the definition to stream that will enable the communication between the application and the standard input/output using streams. These are all of the standard well-known IOStream objects: cin, cout, cerr, clog, and their wide-character companions, wcin, wcout, wcerr, and wclog.