Using the Adapter Classes for Output
The first example of the Streams package’s adapter classes creates a
RWStreambufToCharOutputStream object that connects to an instance of class
RWCharToStreambufOutputStreamImp, which in turn uses an iostreams
filebuf as a sink of narrow characters. The instance of the adapter class
RWStreambufToCharOutputStream is used to construct an iostreams object of type
ostream, which is used as the sink of data in the rest of the example.
Figure 17 is a representation of the chain of streaming elements used in this example.
The complete example is located in directory ...\examples\stream in the file adapterWrite.cpp. Only part of the code is presented below.
filebuf fbuf; // 1
fbuf.open("adaptorWrite.dat", ios::out);
RWCharOutputStream charOutputStream =
RWCharToStreambufOutputStreamImp::make(fbuf); // 2
RWStreambufToCharOutputStream streambufAdaptor(charOutputStream);// 3
ostream os(&streambufAdaptor); // 4
try {
double f= 3.14159;
int i= 567;
char* text="Wagabunga";
// insert a bunch of things
os << f << ' ' << i << ' ' << text; // 5
}
catch(const RWIncompleteStreamOperation& e) { // 6
cout << e.why() << endl;
cout << e.elementsProcessed() << endl;
}
catch(const RWExternalStreamException& e) {
cout << e.why() << endl;
}