# Input/output library

From cppreference.com
< cpp

C++ includes two input/output libraries: a modern, stream-based I/O library and the standard set of C-style I/O functions.

### Stream-based I/O

The stream-based input/output library is organized around abstract input/output devices. These abstract devices allow the same code to handle input/output to files, memory streams, or custom adaptor devices that perform arbitrary operations (e.g. compression) on the fly.

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). The classes are organized into the following hierarchy:

Inheritance diagram

##### Abstraction

Defined in header `<ios>`
ios_base | manages formatting flags and input/output exceptions (class)
basic_ios | manages an arbitrary stream buffer (class template)
Defined in header `<streambuf>`
basic_streambuf | abstracts a raw device (class template)
Defined in header `<ostream>`
basic_ostream | wraps a given abstract device (std::basic_streambuf) and provides high-level output interface (class template)
Defined in header `<istream>`
basic_istream | wraps a given abstract device (std::basic_streambuf) and provides high-level input interface (class template)
basic_iostream | wraps a given abstract device (std::basic_streambuf) and provides high-level input/output interface (class template)

##### File I/O implementation

Defined in header `<fstream>`
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/O implementation

Defined in header `<sstream>`
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

Defined in header `<strstream>`
strstreambuf (deprecated in C++98) | implements raw character array device (class)
istrstream (deprecated in C++98) | implements character array input operations (class)
ostrstream (deprecated in C++98) | implements character array output operations (class)
strstream (deprecated in C++98) | implements character array input/output operations (class)

##### Synchronized output

Defined in header `<syncstream>`
basic_syncbuf (C++20) | synchronized output device wrapper (class template)
basic_osyncstream (C++20) | synchronized output stream wrapper (class template)

#### 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_syncbuf<char>         syncbuf;
    typedef basic_syncbuf<wchar_t>     wsyncbuf;

    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;

    typedef basic_osyncstream<char>     osyncstream;
    typedef basic_osyncstream<wchar_t> wosyncstream;

#### Predefined standard stream objects

Defined in header `<iostream>`
cin wcin | reads from the standard C input stream stdin (global object)
cout wcout | writes to the standard C output stream stdout (global object)
cerr wcerr | writes to the standard C error stream stderr, unbuffered (global object)
clog wclog | writes to the standard C error stream stderr (global object)

#### I/O Manipulators

The stream-based I/O library uses I/O manipulators (e.g. std::boolalpha, std::hex, etc.) to control how streams behave.

#### Types

The following auxiliary types are defined:

Defined in header `<ios>`
streamoff | represents relative file/stream position (offset from fpos), 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)
fpos | represents absolute position in a stream or a file (class template)

The following specializations of std::fpos are provided:

Defined in header `<ios>`
Type | Definition
`streampos` | std::fpos<std::char_traits<char>::state_type>
`wstreampos` | std::fpos<std::char_traits<wchar_t>::state_type>

#### Error category interface

Defined in header `<ios>`
io_errc (C++11) | the IO stream error codes (enum)
iostream_category (C++11) | identifies the iostream error category (function)

### C-style IO

C++ also includes the input/output functions defined by C, such as std::fopen, std::getc, etc.

Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/io&oldid=109849"