Input/output library

From Cppreference

< cpp
Jump to: navigation, search

Standard input/output library is organized in such a way that the underlying device of an input/output operation is abstracted. In this way the same code can handle input/output to a file, memory stream, or custom adaptor device interpreting the data on the fly, e.g. compressing it. Most of the classes are templated, so they can be adapted to any basic character type. Separate typedefs are provided for the most common basic character types (char and wchar_t).

cpp/io/ios base cpp/io/basic ios cpp/io/basic istream cpp/io/basic ostream cpp/io/basic ifstream cpp/io/basic istringstream cpp/io/basic iostream cpp/io/basic ofstream cpp/io/basic ostringstream cpp/io/basic fstream cpp/io/basic stringstreamstd-io-complete-inheritance.png
About this image

Inheritance diagram

Contents

Abstraction
basic_streambuf
abstracts a raw device
(class template)
basic_istream
wraps a given abstract device (std::basic_streambuf)
and provides high-level input interface
(class template)
basic_ostream
wraps a given abstract device (std::basic_streambuf)
and provides high-level output interface
(class template)
basic_iostream
wraps a given abstract device (std::basic_streambuf)
and provides high-level input/output interface
(class template)
File I/0 implementation
basic_filebuf
implements raw file device
(class template)
basic_ifstream
implements high-level file stream input operations
(class template)
basic_ofstream
implements high-level file stream output operations
(class template)
basic_fstream
implements high-level file stream input/output operations
(class template)
String I/0 implementation
basic_stringbuf
implements raw string device
(class template)
basic_istringstream
implements high-level string stream input operations
(class template)
basic_ostringstream
implements high-level string stream output operations
(class template)
basic_stringstream
implements high-level string stream input/output operations
(class template)
Array I/O implementations
strstreambuf (deprecated)
implements raw character array device
(class)
istrstream (deprecated)
implements character array input operations
(class)
ostrstream (deprecated)
implements character array output operations
(class)

[edit] Typedefs

The following typedefs for common character types are provided:

typedef basic_ios<char>                ios;
typedef basic_ios<wchar_t>            wios;
 
typedef basic_streambuf<char>     streambuf;
typedef basic_streambuf<wchar_t> wstreambuf;
typedef basic_filebuf<char>         filebuf;
typedef basic_filebuf<wchar_t>     wfilebuf;
typedef basic_stringbuf<char>     stringbuf;
typedef basic_stringbuf<wchar_t> wstringbuf;
 
typedef basic_istream<char>         istream;
typedef basic_istream<wchar_t>     wistream;
typedef basic_ostream<char>         ostream;
typedef basic_ostream<wchar_t>     wostream;
typedef basic_iostream<char>       iostream;
typedef basic_iostream<wchar_t>   wiostream;
 
typedef basic_ifstream<char>       ifstream;
typedef basic_ifstream<wchar_t>   wifstream;
typedef basic_ofstream<char>       ofstream;
typedef basic_ofstream<wchar_t>   wofstream;
typedef basic_fstream<char>         fstream;
typedef basic_fstream<wchar_t>     wfstream;
 
typedef basic_istringstream<char>     istringstream;
typedef basic_istringstream<wchar_t> wistringstream;
typedef basic_ostringstream<char>     ostringstream;
typedef basic_ostringstream<wchar_t> wostringstream;
typedef basic_stringstream<char>       stringstream;
typedef basic_stringstream<wchar_t>   wstringstream;

Predefined standard stream objects:

extern  istream  cin;   //standard input (stdin)
extern wistream wcin;
extern  ostream  cout;  //standard output (stdout)
extern wostream wcout;
extern  ostream  cerr;  //standard error (stderr)
extern wostream wcerr;
extern  ostream  clog;  //standard log (stdlog)
extern wostream wclog;

[edit] Types

The following auxiliary type are defined:

Defined in header <ios>
fpos
represents a position in a stream
(class template)
streampos
represents a position in a narrow character streams
(class template specialization)
wstreampos
represents a position in wide character streams
(class template specialization)
streamoff
represents offset from a streampos, sufficient to represent any file size
(typedef)
streamsize
represents the number of characters transferred in an I/O operation or the size of an I/O buffer
(typedef)

[edit] Manipulators

[edit] Error category interface

Defined in header <ios>
io_errc (C++11)
the system error category enumeration of IO error codes
(class)
is_error_code_enum(std::io_errc) (C++11)
extends the type trait std::is_error_code_enum to identify iostream error codes
(function template specialization)
iostream_category (C++11)
constructs an iostream error category
(function)
make_error_code(std::io_errc) (C++11)
constructs an iostream error code
(function)
make_error_condition(std::io_errc) (C++11)
constructs an iostream error_condition
(function)

[edit] C-style input/output