|Skip Navigation Links|
|Exit Print View|
|Oracle Solaris Studio 12.3: C++ User's Guide Oracle Solaris Studio 12.3 Information Library|
iostreams are the formatting part of a two-part (input or output) system. The other part of the system is made up of streambuf streams, which work with input or output of unformatted streams of characters.
You usually use streambuf streams through iostreams, so you don’t have to be familiar with them in detail. You can use streambuf streams directly if you choose to, for example, if you need to improve efficiency or to get around the error handling or formatting built into iostreams.
A streambuf consists of a stream or sequence of characters and one or two pointers into that sequence. Each pointer points between two characters. (Pointers cannot actually point between characters, but thinking of them that way can be helpful.) There are two kinds of streambuf pointers:
A streambuf can have one or both of these pointers.
The positions of the pointers and the contents of the sequences can be manipulated in various ways. Whether or not both pointers move when manipulated depends on the kind of streambuf used. Generally, with queue-like streambuf streams, the get and put pointers move independently. With file-like streambuf streams the get and put pointers always move together. A strstream is an example of a queue-like stream; an fstream is an example of a file-like stream.
You never create an actual streambuf object, but only objects of classes derived from class streambuf. Examples are filebuf and strstreambuf, which are described in the filebuf(3CC4) and ssbuf(3) man pages. Advanced users may want to derive their own classes from streambuf to provide an interface to a special device or to provide other than basic buffering. The sbufpub(3CC4) and sbufprot man pages (3CC4) discuss how to do this.
Apart from creating your own special kind of streambuf, you might want to access the streambuf associated with an iostream to access the public member functions, as described in the man pages. In addition, each iostream has a defined inserter and extractor which takes a streambuf pointer. When a streambuf is inserted or extracted, the entire stream is copied.
ifstream fromFile("thisFile"); ofstream toFile ("thatFile"); toFile << fromFile.rdbuf();
The input and output files are opened as before. Every iostream class has a member function rdbuf that returns a pointer to the streambuf object associated with it. In the case of an fstream, the streambuf object is type filebuf. The entire file associated with fromFile is copied (inserted into) the file associated with toFile. The last line could also be written as follows:
fromFile >> toFile.rdbuf();
The source file is then extracted into the destination. The two methods are entirely equivalent.