Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/io"

From cppreference.com
< cpp
m (r2.7.3) (Robot: Adding ar, de, es, fr, pt)
m (Shorten template names. Use {{lc}} where appropriate.)
Line 12: Line 12:
 
{{inheritance diagram/std-io-complete}}
 
{{inheritance diagram/std-io-complete}}
  
{{dcl list begin}}
+
{{dsc begin}}
{{dcl list h2 | Abstraction}}
+
{{dsc h2 | Abstraction}}
{{dcl list class | cpp/io/ios_base | manages formatting flags and input/output exceptions}}
+
{{dsc class | cpp/io/ios_base | manages formatting flags and input/output exceptions}}
{{dcl list tclass | cpp/io/basic_ios | manages an arbitrary stream buffer}}
+
{{dsc tclass | cpp/io/basic_ios | manages an arbitrary stream buffer}}
{{dcl list tclass | cpp/io/basic_streambuf | abstracts a raw device}}
+
{{dsc tclass | cpp/io/basic_streambuf | abstracts a raw device}}
{{dcl list tclass | cpp/io/basic_istream | wraps a given abstract device ({{c|std::basic_streambuf}})<br/> and provides high-level input interface}}
+
{{dsc tclass | cpp/io/basic_istream | wraps a given abstract device ({{lc|std::basic_streambuf}})<br/> and provides high-level input interface}}
{{dcl list tclass | cpp/io/basic_ostream | wraps a given abstract device ({{c|std::basic_streambuf}})<br/> and provides high-level output interface}}
+
{{dsc tclass | cpp/io/basic_ostream | wraps a given abstract device ({{lc|std::basic_streambuf}})<br/> and provides high-level output interface}}
{{dcl list tclass | cpp/io/basic_iostream | wraps a given abstract device ({{c|std::basic_streambuf}})<br/> and provides high-level input/output interface}}
+
{{dsc tclass | cpp/io/basic_iostream | wraps a given abstract device ({{lc|std::basic_streambuf}})<br/> and provides high-level input/output interface}}
{{dcl list h2 | File I/0 implementation}}
+
{{dsc h2 | File I/0 implementation}}
{{dcl list tclass | cpp/io/basic_filebuf | implements raw file device}}
+
{{dsc tclass | cpp/io/basic_filebuf | implements raw file device}}
{{dcl list tclass | cpp/io/basic_ifstream | implements high-level file stream input operations}}
+
{{dsc tclass | cpp/io/basic_ifstream | implements high-level file stream input operations}}
{{dcl list tclass | cpp/io/basic_ofstream | implements high-level file stream output operations}}
+
{{dsc tclass | cpp/io/basic_ofstream | implements high-level file stream output operations}}
{{dcl list tclass | cpp/io/basic_fstream | implements high-level file stream input/output operations}}
+
{{dsc tclass | cpp/io/basic_fstream | implements high-level file stream input/output operations}}
{{dcl list h2 | String I/0 implementation}}
+
{{dsc h2 | String I/0 implementation}}
{{dcl list tclass | cpp/io/basic_stringbuf | implements raw string device}}
+
{{dsc tclass | cpp/io/basic_stringbuf | implements raw string device}}
{{dcl list tclass | cpp/io/basic_istringstream | implements high-level string stream input operations}}
+
{{dsc tclass | cpp/io/basic_istringstream | implements high-level string stream input operations}}
{{dcl list tclass | cpp/io/basic_ostringstream | implements high-level string stream output operations}}
+
{{dsc tclass | cpp/io/basic_ostringstream | implements high-level string stream output operations}}
{{dcl list tclass | cpp/io/basic_stringstream | implements high-level string stream input/output operations}}
+
{{dsc tclass | cpp/io/basic_stringstream | implements high-level string stream input/output operations}}
{{dcl list h2 | Array I/O implementations}}
+
{{dsc h2 | Array I/O implementations}}
{{dcl list class | cpp/io/strstreambuf | notes={{mark deprecated}} | implements raw character array device}}
+
{{dsc class | cpp/io/strstreambuf | notes={{mark deprecated}} | implements raw character array device}}
{{dcl list class | cpp/io/istrstream | notes={{mark deprecated}} | implements character array input operations}}
+
{{dsc class | cpp/io/istrstream | notes={{mark deprecated}} | implements character array input operations}}
{{dcl list class | cpp/io/ostrstream | notes={{mark deprecated}} | implements character array output operations}}
+
{{dsc class | cpp/io/ostrstream | notes={{mark deprecated}} | implements character array output operations}}
{{dcl list class | cpp/io/strstream | notes={{mark deprecated}} | implements character array input/output operations}}
+
{{dsc class | cpp/io/strstream | notes={{mark deprecated}} | implements character array input/output operations}}
{{dcl list end}}
+
{{dsc end}}
  
 
====Typedefs====
 
====Typedefs====
Line 88: Line 88:
 
====[[cpp/io/manip|I/O Manipulators]]====
 
====[[cpp/io/manip|I/O Manipulators]]====
  
The stream-based I/O library uses [[cpp/io/manip| I/O manipulators]] (e.g. {{c|std::boolalpha}}, {{c|std::hex}}, etc.) to control how streams behave.
+
The stream-based I/O library uses [[cpp/io/manip| I/O manipulators]] (e.g. {{lc|std::boolalpha}}, {{lc|std::hex}}, etc.) to control how streams behave.
  
 
====Types====
 
====Types====
  
 
The following auxiliary types are defined:
 
The following auxiliary types are defined:
{{dcl list begin}}
+
{{dsc begin}}
{{dcl list header | ios }}
+
{{dsc header | ios }}
{{dcl list typedef | cpp/io/streamoff | represents relative file/stream position (offset from fpos), sufficient to represent any file size }}
+
{{dsc typedef | cpp/io/streamoff | represents relative file/stream position (offset from fpos), sufficient to represent any file size }}
{{dcl list typedef | cpp/io/streamsize | represents the number of characters transferred in an I/O operation or the size of an I/O buffer}}
+
{{dsc typedef | cpp/io/streamsize | represents the number of characters transferred in an I/O operation or the size of an I/O buffer}}
{{dcl list tclass  | cpp/io/fpos | represents absolute position in a stream or a file}}
+
{{dsc tclass  | cpp/io/fpos | represents absolute position in a stream or a file}}
{{dcl list end}}
+
{{dsc end}}
  
Four specializations of {{c|std::fpos}} are provided:  
+
Four specializations of {{lc|std::fpos}} are provided:  
{{dcl list begin}}
+
{{dsc begin}}
{{dcl list header | ios}}
+
{{dsc header | ios}}
{{dcl list hitem | Type | Definition}}
+
{{dsc hitem | Type | Definition}}
{{dcl list item | {{tt|streampos}} | {{c|std::fpos<std::char_traits<char>::state_type>}}}}
+
{{dsc | {{tt|streampos}} | {{c|std::fpos<std::char_traits<char>::state_type>}}}}
{{dcl list item | {{tt|u16streampos}} | {{c|std::fpos<std::char_traits<char16_t>::state_type>}}}}
+
{{dsc | {{tt|u16streampos}} | {{c|std::fpos<std::char_traits<char16_t>::state_type>}}}}
{{dcl list item | {{tt|u32streampos}} | {{c|std::fpos<std::char_traits<char32_t>::state_type>}}}}
+
{{dsc | {{tt|u32streampos}} | {{c|std::fpos<std::char_traits<char32_t>::state_type>}}}}
{{dcl list item | {{tt|wstreampos}} | {{c|std::fpos<std::char_traits<wchar_t>::state_type>}}}}
+
{{dsc | {{tt|wstreampos}} | {{c|std::fpos<std::char_traits<wchar_t>::state_type>}}}}
{{dcl list end}}
+
{{dsc end}}
  
 
====Error category interface====
 
====Error category interface====
{{dcl list begin}}
+
{{dsc begin}}
{{dcl list header | ios }}
+
{{dsc header | ios }}
{{dcl list template | cpp/io/dcl list io_errc}}
+
{{dsc inc | cpp/io/dcl list io_errc}}
{{dcl list template | cpp/io/dcl list iostream_category}}
+
{{dsc inc | cpp/io/dcl list iostream_category}}
 
<!-- TODO: these three specializations go inside the io_errc page  
 
<!-- TODO: these three specializations go inside the io_errc page  
{{dcl list ptfun | cpp/io/is_error_code_enum  | title=is_error_code_enum{{dcl small|(std::io_errc)}}| notes={{mark c++11}} | extends the type trait {{c|std::is_error_code_enum}} to identify iostream error codes}}
+
{{dcl list ptfun | cpp/io/is_error_code_enum  | title=is_error_code_enum{{dsc small|(std::io_errc)}}| notes={{mark c++11}} | extends the type trait {{lc|std::is_error_code_enum}} to identify iostream error codes}}
{{dcl list fun | cpp/io/make_error_code | title=make_error_code{{dcl small|(std::io_errc)}} | notes={{mark c++11}} | constructs an iostream error code}}
+
{{dsc fun | cpp/io/make_error_code | title=make_error_code{{dsc small|(std::io_errc)}} | notes={{mark c++11}} | constructs an iostream error code}}
{{dcl list fun | cpp/io/make_error_condition  | title=make_error_condition{{dcl small|(std::io_errc)}} | notes={{mark c++11}} | constructs an iostream error_condition}}
+
{{dsc fun | cpp/io/make_error_condition  | title=make_error_condition{{dsc small|(std::io_errc)}} | notes={{mark c++11}} | constructs an iostream error_condition}}
 
-->
 
-->
{{dcl list end}}
+
{{dsc end}}
  
 
===[[cpp/io/c | C-style IO]]===
 
===[[cpp/io/c | C-style IO]]===
  
C++ also includes the [[cpp/io/c | input/output functions defined by C]], such as {{c|std::fopen}}, {{c|std::getc}}, etc.
+
C++ also includes the [[cpp/io/c | input/output functions defined by C]], such as {{lc|std::fopen}}, {{lc|std::getc}}, etc.
  
 
[[ar:cpp/io]]
 
[[ar:cpp/io]]

Revision as of 18:47, 31 May 2013

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

Contents

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:

cpp/io/ios basecpp/io/basic ioscpp/io/basic istreamcpp/io/basic ifstreamcpp/io/basic istringstreamcpp/io/basic ostreamcpp/io/basic ofstreamcpp/io/basic ostringstreamcpp/io/basic fstreamcpp/io/basic stringstreamcpp/io/basic iostreamstd-io-complete-inheritance.svg

Inheritance diagram

Abstraction
manages formatting flags and input/output exceptions
(class)
manages an arbitrary stream buffer
(class template)
abstracts a raw device
(class template)
wraps a given abstract device (std::basic_streambuf)
and provides high-level input interface
(class template)
wraps a given abstract device (std::basic_streambuf)
and provides high-level output interface
(class template)
wraps a given abstract device (std::basic_streambuf)
and provides high-level input/output interface
(class template)
File I/0 implementation
implements raw file device
(class template)
implements high-level file stream input operations
(class template)
implements high-level file stream output operations
(class template)
implements high-level file stream input/output operations
(class template)
String I/0 implementation
implements raw string device
(class template)
implements high-level string stream input operations
(class template)
implements high-level string stream output operations
(class template)
implements high-level string stream input/output operations
(class template)
Array I/O implementations
(deprecated)
implements raw character array device
(class)
(deprecated)
implements character array input operations
(class)
(deprecated)
implements character array output operations
(class)
(deprecated)
implements character array input/output operations
(class)

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;

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>
represents relative file/stream position (offset from fpos), sufficient to represent any file size
(typedef)
represents the number of characters transferred in an I/O operation or the size of an I/O buffer
(typedef)
represents absolute position in a stream or a file
(class template)

Four specializations of std::fpos are provided:

Defined in header <ios>
Type Definition
streampos std::fpos<std::char_traits<char>::state_type>
u16streampos std::fpos<std::char_traits<char16_t>::state_type>
u32streampos std::fpos<std::char_traits<char32_t>::state_type>
wstreampos std::fpos<std::char_traits<wchar_t>::state_type>

Error category interface

Template:cpp/io/dcl list io errcTemplate:cpp/io/dcl list iostream category
Defined in header <ios>

C-style IO

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