Input/output (C++)
![]() | This article has an unclear citation style. The reason given is: Violates Wikipedia:External links: "Wikipedia articles may include links to web pages outside Wikipedia (external links), but they should not normally be used in the body of an article". (June 2013) |
C++ Standard Library |
---|
Containers |
C standard library |
In the C++ programming language, input/output library refers to a family of class templates and supporting functions in the C++ Standard Library that implement stream-based input/output capabilities.[1][2] It is an object-oriented alternative to C's FILE-based streams from the C standard library.[3][4]
@@History @@
Bjarne Stroustrup, the creator of C++, wrote the first version of the stream I/O library in 1984, as a type-safe and extensible alternative to C's I/O library.[5] The library has undergone a number of enhancements since this early version, including the introduction of manipulators to control formatting, and templatization to allow its use with character types other than char
.
Standardization in 1998 saw the library moved into the std
namespace, and the main header changed from <iostream.h>
to <iostream>
. It is this standardized version that is covered in the rest of the article.
@@History @@
Bjarne Stroustrup, the creator of C++, wrote the first version of the stream I/O library in 1984, as a type-safe and extensible alternative to C's I/O library.[6] The library has undergone a number of enhancements since this early version, including the introduction of manipulators to control formatting, and templatization to allow its use with character types other than char
.
Standardization in 1998 saw the library moved into the std
namespace, and the main header changed from <iostream.h>
to <iostream>
. It is this standardized version that is covered in the rest of the article.
Stream buffers
There are twelve stream buffer classes defined in the C++ language as the table.
Support classes
ios_base
and basic_ios
are two classes that manage the lower-level bits of a stream. ios_base
stores formatting information and the state of the stream. basic_ios
manages the associated stream-buffer. basic_ios
is commonly known as simply ios
or wios
, which are two typedefs for basic_ios
with a specific character type. basic_ios
and ios_base
are very rarely used directly by programmers. Usually, their functionality is accessed through other classes such as iostream
which inherit them.[7][8]
Typedefs
Name | description |
---|---|
ios |
convenience typedef for a basic_ios working with characters of type char
|
wios |
convenience typedef for a basic_ios working with characters of type wchar_t
|
streamoff |
supports internal operations. |
streampos |
holds the current position of the buffer pointer or file pointer. |
wstreampos |
holds the current position of the buffer pointer or file pointer. |
streamsize |
specifies the size of the stream. |
Formatting manipulators
Name | Description |
---|---|
boolalpha / noboolalpha
|
specifies whether variables of type bool appear as true and false or as 0 and 1 in the stream.
|
skipws / noskipws
|
specifies whether the white space is skipped in input operations |
showbase / noshowbase
|
specifies whether the notational base of the number is displayed |
showpoint / noshowpoint
|
specifies whether to display the fractional part of a floating point number, when the fractional part is zero |
showpos / noshowpos
|
specifies whether to display + for positive numbers
|
unitbuf / nounitbuf
|
specifies whether the output should be buffered |
uppercase / nouppercase
|
specifies whether uppercase characters should be used in hexadecimal integer and floating-point output |
left / right / internal
|
specifies how a number should be justified |
dec / oct / hex
|
specifies the notation an integer number should be displayed in |
fixed / scientific /hexfloat (C++11) / defaultfloat (C++11)
|
specifies the notation a floating-point number should be displayed in |
Input/output streams
![]() | This article may require cleanup to meet Wikipedia's quality standards. The specific problem is: Talks about a header, when it should talk about input/output streams. (March 2012) |
C++ input/output streams are primarily defined by iostream
, a header file that is part of the C++ 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 the cstdio
header inherited from C's stdio.h, iostream
provides basic input and output services for C++ programs. iostream uses the objects cin
, cout
, cerr
, and clog
for sending data to and from the standard streams input, output, error (unbuffered), and log (buffered) respectively. As part of the C++ standard library, these objects are a part of the std
namespace.[9]
The cout
object is of type ostream
, which overloads the left bit-shift operator to make it perform an operation completely unrelated to bitwise operations, and notably evaluate to the value of the left argument, allowing multiple operations on the same ostream object, essentially as a different syntax for method cascading, exposing a fluent interface. The cerr
and clog
objects are also of type ostream
, so they overload that operator as well. The cin
object is of type istream
, 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.
Output formatting
Methods
width(int x) |
minimum number of characters for next output |
fill(char x) |
character used to fill with in the case that the width needs to be elongated to fill the minimum. |
precision(int x) |
sets the number of significant digits for floating-point numbers |
Manipulators
Manipulators are objects that can modify a stream using the <<
or >>
operators.
endl |
"end line": inserts a newline into the stream and calls flush. |
ends |
"end string": inserts a null character into the stream and calls flush. |
flush |
forces an output stream to write any buffered characters |
ws |
causes an inputstream to 'eat' whitespace |
showpoint |
tells the stream to show the decimal point and some zeros with whole numbers |
Other manipulators can be found using the header iomanip
.
Criticism
Some environments do not provide a shared implementation of the C++ library. These include embedded systems and Windows systems running programs built with MinGW. Under these systems, the C++ standard library must be statically linked to a program, which increases the size of the program,[10] or distributed as a shared library alongside the program.
Some implementations of the C++ standard library have significant amounts of dead code. For example, GNU libstdc++ automatically constructs a locale when building an ostream
even if a program never uses any types (date, time or money) that a locale affects,[11]
and a statically linked "Hello, World!" program that uses <iostream>
of GNU libstdc++ produces an executable an order of magnitude larger than an equivalent program that uses <cstdio>
.[12]
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.[13]
Naming conventions
Examples
The canonical "Hello, World!" program can be expressed as follows:
#include <iostream>
int main()
{
std::cout << "Hello, world!" << std::endl;
}
This program would output "Hello, world!" followed by a newline and standard output stream buffer flush.
The following example creates a file called 'file.txt' and puts the text 'Hello, world!' followed by a newline into it.
#include <fstream>
int main()
{
std::ofstream file("file.txt");
file << "Hello, world!" << std::endl;
}
References
- ^ ISO/IEC 14882:2003 Programming Languages — C++. [lib.string.streams]/1
- ^ Stanley B. Lippman, Josee Lajoie (1999). C++ Primer (third ed.). Massachusetts: Addison-Wesley. pp. 1109–1112. ISBN 0-201-82470-1.
- ^ Bjarne Stroustrup (1997). The C++ programming language (third ed.). Addison-Wesley. pp. 637–640. ISBN 0-201-88954-4.
- ^ Stanley B. Lippman, Josee Lajoie (1999). C++ Primer (third ed.). Massachusetts: Addison-Wesley. pp. 1063–1067. ISBN 0-201-82470-1.
- ^ Bjarne Stroustrup. "A History of C++: 1979–1991" (PDF).
- ^ Bjarne Stroustrup. "A History of C++: 1979–1991" (PDF).
- ^ Stanley B. Lippman, Josee Lajoie (1999). C++ Primer (third ed.). Massachusetts: Addison-Wesley. pp. 1112–1120. ISBN 0-201-82470-1.
- ^ "<ios> Visual Studio 2010". Microsoft MSDN: Visual Studio 2010. Retrieved 28 September 2011.
- ^ Holzner, Steven (2001). C++ : Black Book. Scottsdale, Ariz.: Coriolis Group. p. 584. ISBN 1-57610-777-9.
...endl, which flushes the output buffer and sends a newline to the standard output stream.
- ^ "MinGW.org: Large executables". Retrieved 22 April 2009.
- ^ GNU libstdc++ source code,
bits/ios_base.h
- ^ C++ vs. C - Pin Eight
- ^ "uClibc++ C++ library". Retrieved 6 January 2012.