Online Book Reader

Home Category

Professional C__ - Marc Gregoire [266]

By Root 1417 0
both input streams and output streams. The header also declares the standard console output stream, cout.

The << operator is the simplest way to use output streams. C++ basic types, such as ints, pointers, doubles, and characters, can be output using <<. In addition, the C++ string class is compatible with <<, and C-style strings are properly output as well. Following are some examples of using <<:

int i = 7;

cout << i << endl;

char ch = 'a';

cout << ch << endl;

string myString = "Marni is adorable.";

cout << myString << endl;

Code snippet from OutputBasics\OutputBasics.cpp

The output is as follows:

7

a

Marni is adorable.

The cout stream is the built-in stream for writing to the console, or standard output. You can “chain” uses of << together to output multiple pieces of data. This is because the << operator returns a reference to the stream as its result so you can immediately use << again on the same stream. For example:

int j = 11;

cout << "On a scale of 1 to cute, Marni ranks " << j << "!" << endl;

Code snippet from OutputBasics\OutputBasics.cpp

The output is as follows:

On a scale of 1 to cute, Marni ranks 11!

C++ streams will correctly parse C-style escape codes, such as strings that contain \n, but it is much more hip to use the built-in endl mechanism for this purpose. The following example uses endl, which is defined in the std namespace to represent an end-of-line character and to flush the output buffer. Several lines of text are output using one line of code.

cout << "Line 1" << endl << "Line 2" << endl << "Line 3" << endl;

Code snippet from OutputBasics\OutputBasics.cpp

The output is as follows:

Line 1

Line 2

Line 3

Methods of Output Streams

The << operator is, without a doubt, the most useful part of output streams. However, there is additional functionality to be explored. If you take a peek at the header file, you’ll see many lines of overloaded definitions of the << operator. You’ll also find some useful public methods.

put() and write()

put() and write() are raw output methods. Instead of taking an object or variable that has some defined behavior for output, put() accepts a single character, while write() accepts a character array. The data passed to these methods is output as is, without any special formatting or processing. For example, the following function takes a C-style string and outputs it to the console without using the << operator:

void rawWrite(const char* data, int dataSize)

{

cout.write(data, dataSize);

}

Code snippet from Write\Write.cpp

The next function writes the given index of a C-style string to the console by using the put() method:

void rawPutChar(const char* data, int charIndex)

{

cout.put(data[charIndex]);

}

Code snippet from Put\Put.cpp

flush()

When you write to an output stream, the stream does not necessarily write the data to its destination right away. Most output streams buffer, or accumulate data instead of writing it out as it comes in. The stream will flush, or write out the accumulated data, when one of the following conditions occurs:

A sentinel, such as the endl marker, is reached.

The stream goes out of scope and is destructed.

Input is requested from a corresponding input stream (i.e., when you make use of cin for input, cout will flush). In the section on file streams, you will learn how to establish this type of link.

The stream buffer is full.

You explicitly tell the stream to flush its buffer.

One way to explicitly tell a stream to flush is to call its flush() method, as in the code that follows:

cout << "abc";

cout.flush(); // abc is written to the console.

cout << "def";

cout << endl; // def is written to the console.

Code snippet from Flush\flush.cpp

Not all output streams are buffered. The cerr stream, for example, does not buffer its output.

Handling Output Errors

Output errors can arise in a variety of situations. Perhaps you are trying to open a non-existing file. Maybe a disk error has prevented a write operation from succeeding, for example because the

Return Main Page Previous Page Next Page

®Online Book Reader