13.29 <iostream>

The <iostream> header declares the eight standard stream objects: cerr, cin, clog, cout, wcerr, wcin, wclog, and wcout. These objects are initialized when the first instance of ios_base::Init is constructed (or earlier), or before the main program starts. They are not destroyed during normal program execution, so any static object's destructor or other function can use the standard I/O objects.

Each of the standard I/O objects is associated with a standard C FILE pointer (see <cstdio>). (You can sever the connection by calling ios_base::sync_with_stdio(false), as described in <ios>.) You can use narrow or wide I/O objects, but once you have performed any I/O on an underlying C stream, you cannot switch from narrow to wide or wide to narrow. For example, after writing to cerr, you cannot write to wclog because both objects use the same C stream, stderr. See Chapter 9 for more information about I/O.

Many C++ programmers assume that <iostream> automatically #includes <istream> and <ostream>, but the standard does not guarantee that behavior. Always #include every header you need, for example:

#include <cstdlib>

#include <iostream>

#include <istream>

#include <ostream>

   

// Copy standard input to standard output.

int main(  )

{

  std::cout << std::cin.rdbuf(  );

  return std::cout ? EXIT_SUCCESS : EXIT_FAILURE;

}
cerr variable Error message stream

extern ostream cerr

The cerr object is a standard output stream associated with the C stderr file. It is typically used for error messages. When the cerr object is initialized, it sets the unitbuf flag, causing cerr to flush the output buffer after every operation.

See Also

clog variable, wcerr variable, ostream in <ostream>

cin variable Normal input stream

extern istream cin

The cin object is a standard input stream associated with the C stdin file. It is used for normal program input. It is tied to cout (see the tie member function of basic_ios in <ios>).

See Also

wcin variable, basic_ios in <ios>, istream in <istream>

clog variable Log output stream

extern ostream clog

The clog object is a standard output stream associated with the C stderr file. Unlike cerr, the clog object does not set the unitbuf flag. This makes it more suitable for writing less critical messages that are not considered normal program output, such as debugging or logging messages.

See Also

cerr variable, wclog variable, ostream in <ostream>

cout variable Normal output stream

extern ostream cout

The cout object is a standard input stream associated with the C stdout file. It is used for normal program output.

See Also

wcout variable, ostream in <ostream>

wcerr variable Wide error message stream

extern wostream wcerr

The wcerr object is a wide character standard output stream associated with the C stderr file. It is typically used for error messages. When the wcerr object is initialized, it sets the unitbuf flag, causing wcerr to flush the output buffer after every operation.

See Also

cerr variable, wclog variable, wostream in <ostream>

wcin variable Wide input stream

extern wistream wcin

The wcin object is a wide character standard input stream associated with the C stdin file. It is used for wide program input. It is tied to wcout (see the tie( ) member function of basic_ios in <ios>).

See Also

cin variable, basic_ios in <ios>, wistream in <istream>

wclog variable Wide log output stream

extern wostream wclog

The wclog object is a wide character standard output stream associated with the C stderr file. Unlike wcerr, the wclog object does not set the unitbuf flag. This makes it more suitable for writing less critical messages that are not considered normal program output, such as debugging or logging messages.

See Also

clog variable, wcerr variable, wostream in <ostream>

wcout variable Wide output stream

extern wostream wcout

The wcout object is a wide character standard input stream associated with the C stdout file. It is used for wide program output.

See Also

cout variable, wostream in <ostream>