Output Streams


For the latest documentation on Visual Studio 2017 RC, see Visual Studio 2017 RC Documentation.

An output stream object is a destination for bytes. The three most important output stream classes are ostream, ofstream, and ostringstream.

The ostream class, through the derived class basic_ostream, supports the predefined stream objects:

  • cout standard output

  • cerr standard error with limited buffering

  • clog similar to cerr but with full buffering

Objects are rarely constructed from ostream; predefined objects are generally used. In some cases, you can reassign predefined objects after program startup. The ostream class, which can be configured for buffered or unbuffered operation, is best suited to sequential text-mode output. All functionality of the base class, ios, is included in ostream. If you construct an object of class ostream, you must specify a streambuf object to the constructor.

The ofstream class supports disk file output. If you need an output-only disk, construct an object of class ofstream. You can specify whether ofstream objects accept binary or text-mode data when constructing the ofstream object or when calling the open member function of the object. Many formatting options and member functions apply to ofstream objects, and all functionality of the base classes ios and ostream is included.

If you specify a filename in the constructor, that file is automatically opened when the object is constructed. Otherwise, you can use the open member function after invoking the default constructor.

Like the run-time function sprintf_s, the ostringstream class supports output to in-memory strings. To create a string in memory by using I/O stream formatting, construct an object of class ostringstream.

Constructing Output Stream Objects

Using Insertion Operators and Controlling Format

Output File Stream Member Functions

Effects of Buffering

Binary Output Files

Overloading the << Operator for Your Own Classes

Writing Your Own Manipulators Without Arguments

iostream Programming