Skip Navigation Links | |
Exit Print View | |
Oracle Solaris Studio 12.3: C++ User's Guide Oracle Solaris Studio 12.3 Information Library |
3. Using the C++ Compiler Options
6. Creating and Using Templates
9. Improving Program Performance
10. Building Multithreaded Programs
12. Using the C++ Standard Library
13. Using the Classic iostream Library
13.3 Using the Classic iostream Library
13.3.1.1 Defining Your Own Insertion Operator
13.3.1.2 Handling Output Errors
13.3.3 Defining Your Own Extraction Operators
13.3.4 Using the char* Extractor
13.3.5 Reading Any Single Character
13.3.10 Using iostreams With stdio
13.4.1 Dealing With Files Using Class fstream
13.4.1.2 Declaring an fstream Without Specifying a File
13.4.1.3 Opening and Closing Files
13.4.1.4 Opening a File Using a File Descriptor
13.4.1.5 Repositioning Within a File
13.7.1 Using Plain Manipulators
13.7.2 Parameterized Manipulators
13.8 strstream: iostreams for Arrays
13.9 stdiobuf: iostreams for stdio Files
13.10 Working Withstreambuf Streams
13.10.1 streambuf Pointer Types
By including the iostream library, a program can use any number of input or output streams. Each stream has some source or sink, which may be one of the following:
Standard input
Standard output
Standard error
A file
An array of characters
A stream can be restricted to input or output, or a single stream can allow both input and output. The iostream library implements these streams using two processing layers.
The lower layer implements sequences, which are simply streams of characters. These sequences are implemented by the streambuf class, or by classes derived from it.
The upper layer performs formatting operations on sequences. These formatting operations are implemented by the istream and ostream classes, which have as a member an object of a type derived from class streambuf. An additional class, iostream, is for streams on which both input and output can be performed.
Standard input, output, and error are handled by special class objects derived from class istream or ostream.
The ifstream, ofstream, and fstream classes, which are derived from istream, ostream, and iostream respectively, handle input and output with files.
The istrstream, ostrstream, and strstream classes, which are derived from istream, ostream, and iostream respectively, handle input and output to and from arrays of characters.
When you open an input or output stream, you create an object of one of these types, and associate the streambuf member of the stream with a device or file. You generally do this association through the stream constructor, so you don’t work with the streambuf directly. The iostream library predefines stream objects for the standard input, standard output, and error output, so you don’t have to create your own objects for those streams.
You use operators or iostream member functions to insert data into a stream (output) or extract data from a stream (input), and to control the format of data that you insert or extract.
When you want to insert and extract a new data type—one of your classes—you generally overload the insertion and extraction operators.