Handling Errors Using the Stream Status Functions
The following code shows how to handle errors using the stream status functions:
 
filebuf fbuf;
fbuf.open("someFile", ios::in); // 1
 
RWCharInputStream charInputStream =
RWCharFromStreambufInputStreamImp::make(fbuf); // 2
 
RWCString line;
RWSize lineNumber=1;
 
while(1) {
line= "";
charInputStream.readUntil(line,'\n'); // 3
if(charInputStream.isGood()) // 4
cout << lineNumber++ << ": " << line << endl;
else
break;
}
if(charInputStream.isEof()) // 5
cout << "We have reached EOF" << endl;
//1 Creates an iostreams file buffer, and opens it in input mode. If you built the Advanced Tools Module with the Standard iostreams library, you need to qualify filebuf and other iostreams elements with std::, or you need to include using declarations.
//2 Creates a concrete instance of class RWCharFromStreambufInputStreamImp. The class is created by calling its static member function make(), which creates an instance of self and returns it as a narrow character input stream handle.
//3 Reads a line of text, and stores it in the RWCString line variable.
//4 Checks for the success of the previous operation.
//5 Checks for failure caused by eof.
Other errors are reported by throwing an instance of one of the two exception classes provided by the Streams package.
Classes RWxmsg and RWExternalErr are included in the Essential Tools Module.
Figure 19 – Exception classes in the Streams package