Online Book Reader

Home Category

Professional C__ - Marc Gregoire [267]

By Root 1308 0
disk is full. None of the streams’ code you have read up until this point has considered these possibilities, mainly for brevity. However, it is vital that you address any error conditions that occur.

When a stream is in its normal usable state, it is said to be “good.” The good() method can be called directly on a stream to determine whether or not the stream is currently good.

if (cout.good()) {

cout << "All good" << endl;

}

The good() method provides an easy way to obtain basic information about the validity of the stream, but it does not tell you why the stream is unusable. There is a method called bad() that provides a bit more information. If bad() returns true, it means that a fatal error has occurred (as opposed to any nonfatal condition like end-of-file). Another method, fail(), returns true if the most recent operation has failed, implying that the next operation will also fail. For example, after calling flush() on an output stream, you could call fail() to make sure the stream is still usable.

cout.flush();

if (cout.fail()) {

cerr << "Unable to flush to standard out" << endl;

}

You can also tell the streams to throw exceptions when a failure occurs. You then write a catch handler to catch ios_base::failure exceptions on which you can use the what() method to get a description of the error and the code() method to get the error code. However, whether or not you get useful information is compiler-dependent:

cout.exceptions(ios::failbit | ios::badbit | ios::eofbit);

try {

cout << "Hello World." << endl;

} catch (const ios_base::failure& ex) {

cerr << "Caught exception: " << ex.what()

<< ", error code = " << ex.code() << endl;

}

Code snippet from Exceptions\Exceptions.cpp

To reset the error state of a stream, use the clear() method:

cout.clear();

Error checking is performed less frequently for console output streams than for file output streams or input streams. The methods discussed here apply for other types of streams as well and are revisited later as each type is discussed.

Output Manipulators

One of the unusual features of streams is that you can throw more than just data down the chute. C++ streams also recognize manipulators, objects that make a change to the behavior of the stream instead of, or in addition to, providing data for the stream to work with.

You have already seen one manipulator: endl. The endl manipulator encapsulates data and behavior. It tells the stream to output an end-of-line sequence and to flush its buffer. Following are some other useful manipulators, many of which are defined in the and standard header files. The example after this list shows how to use them:

boolalpha and noboolalpha. Tells the stream to output bool values as true and false (boolalpha) or 1 and 0 (noboolalpha). The default is noboolalpha.

hex, oct, and dec. Outputs numbers in hexadecimal, octal, and base 10, respectively.

setprecision. Sets the number of decimal places that are output for fractional numbers. This is a parameterized manipulator (meaning that it takes an argument).

setw. Sets the field width for outputting numerical data. This is a parameterized manipulator.

setfill. Specifies the character that is used to pad numbers that are smaller than the specified width. This is a parameterized manipulator.

showpoint and noshowpoint. Forces the stream to always or never show the decimal point for floating point numbers with no fractional part.

put_money. Writes a formatted money amount to a stream.

put_time. Writes a formatted time to a stream.

The following example uses several of these manipulators to customize its output. The example also uses the concept of locales, discussed in Chapter 14.

// Boolean values

bool myBool = true;

cout << "This is the default: " << myBool << endl;

cout << "This should be true: " << boolalpha << myBool << endl;

cout << "This should be 1: " << noboolalpha << myBool << endl;

// Simulate "%6d" with streams

int i = 123;

printf("This should be ' 123': %6d\n", i);

cout << "This should be ' 123': " << setw(6) << i <<

Return Main Page Previous Page Next Page

®Online Book Reader