Namespaces
Variants
Views
Actions

Input/output library

From cppreference.com
< cpp
Revision as of 14:45, 6 June 2011 by P12 (Talk | contribs)

Template:cpp/io/sidebar

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 (Template:cpp and Template:cpp).

The library consists of the following elements:

  • Abstraction:
  • raw device abstraction class:
  • classes that act as a wrapper for a given abstract device and provide high level input/output interface:
These classes internally inherit several utility classes:
  • Implementation:
  • file based streams:
  • string based streams:


Contents

Abstractions

Raw device

The interface with underlying raw device is abstracted by abstract class template basic_streambuf. This class is not meant to be copyable: one basic_streambuf object corresponds to one actual device.

Device management

Class template basic_ios manages an object that implements basic_streambuf interface. Several basic_ios objects can refer to one actual basic_streambuf object. basic_ios stores error and formatting flags. A lot of this functionality is placed in ios_base base class.

Streams

The class templates basic_istream, basic_ostream and basic_iostream provide high level input/output capabilities such as sequential reading/writing as well as formatting. These classes all implement that functionality over interface, provided by the basic_streambuf class. It is accessed through basic_ios class, which basic_istream, basic_ostream and basic_iostream inherit. The capabilities of these classes are as follows:

Implementations

Raw devices

Two classes that implements the basic_streambuf interface are provided:

Streams

Three class templates pair basic_istream, basic_ostream and basic_iostream with basic_filebuf:

  • class template basic_ifstream provides support for file input operations.
  • class template basic_ofstream provides support for file output operations.
  • class template basic_fstream provides support for both file input and output operations.

Three class templates pair basic_istream, basic_ostream and basic_iostream with basic_stringbuf:

Typedefs

The following typedefs for common character types are provided:

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;

Manipulators

Several manipulators are defined to control the formatting of input/output operations on streams.


Some of the behavior of the C++ I/O streams (precision, justification, etc) may be modified by manipulating various I/O stream format flags.

|exceptions|set the stream to throw exceptions on errors| |rdstate|returns the state flags of the stream|

C-style input/output

C-style input/output