Using the Adapter Classes for Input
The following example implements the previous example’s corresponding input operation. It creates a
RWStreambufFromCharInputStream object that connects to an instance of class
RWCharFromStreambufInputStreamImp, which in turn uses an iostreams
filebuf as a source of narrow characters. The instance of the adapter class
RWStreambufFromCharInputStream is used to construct an iostreams object of type
istream, which is used as the source of data in the rest of the example.
Figure 18 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 adapterRead.cpp. Only part of the code is presented below.
filebuf fbuf; // 1
fbuf.open("adaptorWrite.dat", ios::in);
RWCharInputStream charInputStream =
RWCharFromStreambufInputStreamImp::make(fbuf); // 2
RWStreambufFromCharInputStream streambufAdaptor(charInputStream);// 3
istream is(&streambufAdaptor); // 4
try {
double f;
int i;
char text[40];
// extract a bunch of things
is >> f >> i >> text; // 5
cout << '\n' << f << '\n' << i << '\n' << text << '\n' << endl;
}
catch(const RWIncompleteStreamOperation& e) { // 6
cout << e.why() << endl;
cout << e.elementsProcessed() << endl;
}
catch(const RWExternalStreamException& e) {
cout << e.why() << endl;
}