- Iostream
iostream
is aheader file which is used forinput/output in theC++ programming language. It is part of theC++ standard library . The name stands for Input/Output Stream. In C++ and its predecessor, the C programming language, there is no special syntax for streaming data input or output. Instead, these are combined as a library of functions. Like thecstdio
header inherited from C'sstdio.h ,iostream
provides basic input and output services for C++ programs. iostream uses the objectscin
,cout
,cerr
, andclog
for sending data to and from thestandard streams input, output, error (unbuffered), and error (buffered) respectively. As part of the C++ standard library, these objects are a part of thestd
namespace.The canonical
Hello world program can be expressed as follows:This program would output "Hello, world!" followed by anewline .The
cout
object is of typeostream
, which overloads the left bit-shift operator to make it perform an operation completely unrelated tobitwise operations . Thecerr
andclog
objects are also of typeostream
, so they overload that operator as well. Thecin
object is of typeistream
, which overloads the right bit-shift operator. The directions of the bit-shift operators make it seem as though data is flowing towards the output stream or flowing away from the input stream.A commonly-used alternative to the newline character
endl
, which is used as follows:endl
is an output manipulator that writes a newline and flushes the buffer, ensuring that the data is output immediately. Several other manipulators are listed below.Output formatting
Methods
Example:
Manipulators
Manipulators are objects that can modify a stream using the
<<
or>>
operators.Other manipulators can be found using the header
.iomanip Criticism
In some environments, such as
MinGW andembedded system s, the C++ standard library must be statically linked to a program, either because of licensing issues [ [http://www.mingw.org/mingwfaq.shtml#faq-cpp-size MinGW Frequently Asked Questions: Why is my C++ binary so large?] ] or because of the lack of a C++ library in the system's firmware.Some implementations of the C++ standard library, such as GNU libstdc++, automatically construct alocale when building anostream
even if no types that could be affected by a locale (such as date, time, and money) are ever used. [GNU libstdc++ source code,bits/ios_base.h
] For instance, a statically-linkedhello world program with the GNU implementation of the C++ standard library produces an executable anorder of magnitude larger than an equivalent program that uses<cstdio>
. [http://pineight.com/rant/#cbloatbloat Pin Eight: RAnT (Rants, Articles, and Treatises) ] ] There exist partial implementations of the C++ standard library designed for space-constrained environments; their<iostream>
may leave out features that programs in such environments may not need, such as locale support. [ [http://cxx.uclibc.org/ uClibc++ C++ library] ]References
External links
* [http://incubator.apache.org/stdcxx/doc/stdlibug/VIII.html Apache C++ Standard Library Iostreams]
* [http://www.informit.com/articles/article.aspx?p=170770 Comprehensive tutorial on formatting output in C++.]
Wikimedia Foundation. 2010.