PRODUCT : Borland C++ NUMBER : 1383 VERSION : ALL OS : ALL DATE : October 25, 1993 PAGE : 1/6 TITLE : Streams: Part 1 -- Introduction Streams : Part 1 : Introduction A stream is something by which information is moved from a source to a destination. The stream therefore is the conduit by which information passes. This conduit is what does the actual work. The source of the information is referred to as the producer (sometimes called the source), the destination of the information is the consumer (sometimes called the sink). Visually you can better understand this concept: Ex 1 Producer Stream Operator Consumer [ keyboard ] { cin } >> int anInteger Ex 2 Consumer Stream Operator Producer [ display ] { cout } << int anInteger The stream, regardless of direction, remains in the same position, it is always left of the operator. The producer and consumer switch position depending on the operation and stream. The >> symbol is the extraction operator. You can imagine the flow of information from the stream into the pointed to destination or consumer. One stream is named "cin" (more on cin later), pronounced sin. Taking one step further we can tie this all together by stating: the keyboard is producing into the stream "cin" and the extraction operator is being used to extract from the "cin" stream into the integer "anInteger" consumer. As explained above, the stream performs the work. The << symbol is the insertion operator. Conversely we can state something similar about example 2: the producer integer "anInteger" is inserting into the stream "cout" and the display consumes the information from "cout". Now for actual code for the examples above: #include Ex1() { int anInteger; PRODUCT : Borland C++ NUMBER : 1383 VERSION : ALL OS : ALL DATE : October 25, 1993 PAGE : 2/6 TITLE : Streams: Part 1 -- Introduction cin >> anInteger; } Ex2() { int a; cout << anInteger; } As you can see the producer from Ex1, and the consumer from Ex2 are omitted. This is a result of the streams being used. The streams "cin" and "cout" are instantiated classes. These classes perform the work needed to move the information. The extraction and insertion operators are overloaded operators of these objects. There is a two layer abstraction for streams. These layers are buffering and formatting. The buffering is handled by the "streambuf" class and the formatting is handled by one of the "ios" class descendants. All "ios" stream classes contain an instantiation of a "streambuf" class or one of its descendants to perform their buffering needs. The "streambuf" class has all the needed functionality for buffering, inserting and extracting characters. Thirty public member functions are available for manipulating buffers. This provides a comprehensive interface to the class. These member functions keep track of pointer positions within the buffer, determine the length of the buffer, return characters from the buffer, keep track of the number of characters left in the buffer and several other needed operations. Three classes which are specialized derived versions of the "streambuf" class are "filebuf", "strstreambuf" and "conbuf". streambuf | --------------------------------------------- | | | filebuf strstreambuf conbuf The "filebuf" class supports file input and output through file descriptors. The "strstreambuf" class stores and fetches characters from arrays of bytes in memory. The "conbuf" class supports output to the console. PRODUCT : Borland C++ NUMBER : 1383 VERSION : ALL OS : ALL DATE : October 25, 1993 PAGE : 3/6 TITLE : Streams: Part 1 -- Introduction The "ios" class has the common variables and functions dealing with keeping track of the current formatting and error states. Four classes are specialized derived versions of the "ios" class, they are "istream", "ostream", "strstreambase" and "fstreambase". Each class adds new functionality and formatting capabilities used by the different input and output mechanisms. ios | --------------------------------------------- | | istream ostream------ | | | --------------------------------------------- constream | | | istream_withassign iostream ostream_withassign | iostream_withassign The "istream" class supports formatted and unformatted conversion of characters extracted from a streambuf. The "ostream" class supports formatted and unformatted conversion of characters inserted into a streambuf. The "iostream" combines the operations for both the "istream" and "ostream" classes. This is intended for formatted and unformatted conversion of characters to and from streambufs. The classes using the _withassign suffix are simply the same as the parent class with an assignment operator overloaded. PRODUCT : Borland C++ NUMBER : 1383 VERSION : ALL OS : ALL DATE : October 25, 1993 PAGE : 4/6 TITLE : Streams: Part 1 -- Introduction ios | --------------------------------------------- | | | istream fstreambase ostream | | | | | | | ----|---- | ------------------| | |------------------ | | | ifstream | ofstream | | | | iostream | | | | | | |--------- | fstream The "fstreambase" class provides operations which are common to file streams. Derived from this class are the "ifstream", "ofstream" and "fstream" classes. These classes perform formatted and unformatted file input and output operations using a "filebuf" class rather than a "streambuf" class. PRODUCT : Borland C++ NUMBER : 1383 VERSION : ALL OS : ALL DATE : October 25, 1993 PAGE : 5/6 TITLE : Streams: Part 1 -- Introduction ios | --------------------------------------------- | | | istream strstreambase ostream | | | | | | | ----|---- | ------------------| | |------------------ | | | istrstream | ostrstream | | | | iostream | | | | | | |--------- | strstream The "strstreambase" class provides operations which are common to supporting in memory formatting of characters. Derived from this class are the "istrstream", "ostrstream" and "strstream" classes. These classes perform formatted and unformatted input and output operations using a "strstreambuf" class rather than a "streambuf" class. ios | ostream | constream Lastly, the "constream" class provides formatted console output operations. This is a Borland specific implementation. There is a set of predefined classes. Remember from above the classes named "cin and "cout", well in addition to those there are also "cerr" and "clog" objects. These objects are defined as follows: PRODUCT : Borland C++ NUMBER : 1383 VERSION : ALL OS : ALL DATE : October 25, 1993 PAGE : 6/6 TITLE : Streams: Part 1 -- Introduction Name Type Description ---- ----- --------------------------------------------------------------- cin istream_withassign standard input, file descriptor 0. Normally the keyboard. cout ostream_withassign standard output, file descriptor 1. Normally the PC's display. cerr ostream_withassign standard error, file descriptor 2. clog ostream_withassign standard error, file descriptor 2, with buffering. The "cin", "cerr" and "clog" objects are also tied to the "cout" object. The objects are predefined by the system, therefore you do not need to create or destroy these objects. This introduction covered the concepts, terminology and hierarchy of streams. For more information on streams please see the following list: Part 1 : Introduction Part 2 : Built-in Types and Manipulators Part 3 : Error Checking Part 4 : User Classes Part 5 : File Input and Output Part 6 : In-Memory Formatting Part 7 : Printing DISCLAIMER: You have the right to use this technical information subject to the terms of the No-Nonsense License Statement that you received with the Borland product to which this information pertains.