📄 arc_7049.htm
字号:
<HTML><HEAD><TITLE>2.2 The Architecture of Iostreams</TITLE></HEAD><BODY><A HREF="ug2.htm"><IMG SRC="images/banner.gif"></A><BR><A HREF="how_0298.htm"><IMG SRC="images/prev.gif"></A><A HREF="booktoc2.htm"><IMG SRC="images/toc.gif"></A><A HREF="for_5394.htm"><IMG SRC="images/next.gif"></A><BR><STRONG>Click on the banner to return to the user guide home page.</STRONG><H2>2.2 The Architecture of Iostreams</H2><P>This section will introduce you to iostreams: what they are, how they work, what kinds of problems they help solve, and how they are structured. <A HREF="arc_7049.htm#2.2.4">Section 2.2.4</A> provides an overview of the class templates in iostreams. If you want to skip over the software architecture of iostreams, please go on to Section 2.3 on formatted input/output.</P><A NAME="2.2.1"><H3>2.2.1 What Are the Standard Iostreams?</H3></A><P>The Standard C++ Library includes classes for text stream input/output. Before the current ANSI/ISO standard, most C++ compilers were delivered with a class library commonly known as the i<I>ostreams </I>library. In this section, we refer to this library as the <I>traditional iostreams</I>, in contrast to the<I> standard iostreams</I> that are now part of the ANSI/ISO Standard C++ Library. The standard iostreams are to some extent compatible with the traditional iostreams, in that the overall architecture and the most commonly used interfaces are retained. <A HREF="how_0298.htm">Section 2.1</A>4 describes the incompatibilities in greater detail.</P><P>We can compare the standard iostreams not only with the traditional C++ iostreams library, but also with the I/O support in the Standard C Library. Many former C programmers still prefer the input/output functions offered by the C library, often referred to as <I>C </I>stdio. Their familiarity with the C library is justification enough for using the C stdio instead of C++ iostreams, but there are other reasons as well. For example, calls to the C functions <SAMP>printf()</SAMP> and <SAMP>scanf()</SAMP> are admittedly more concise with C stdio. However, C stdio has drawbacks, too, such as type insecurity and inability to extend consistently for user-defined classes. We'll discuss these in more detail in the following sections.</P><A NAME="2.2.1.1"><H4>2.2.1.1 Type Safety</H4></A><P>Let us compare a call to stdio functions with the use of standard iostreams. The stdio call reads as follows:</P><PRE>int i = 25;char name[50] = "Janakiraman";fprintf(stdout, "%d %s", i, name);</PRE><P>It correctly prints: <SAMP>25 Janakiraman</SAMP>.</P><P>But what if we inadvertently switch the arguments to <SAMP>fprintf</SAMP>? The error will be detected no sooner than run time. Anything can happen, from peculiar output to a system crash. This is not the case with the standard iostreams:</P><PRE>cout << i << ' ' << name << '\n';</PRE><P>Since there are overloaded versions of the shift operator <SAMP>operator<<()</SAMP>, the right operator will always be called. The function <SAMP>cout << i </SAMP>calls <SAMP>operator<<(int)</SAMP>, and <SAMP>cout << name</SAMP> calls <SAMP>operator<<(const char*)</SAMP>. Hence, the standard iostreams are typesafe.</P><A NAME="2.2.1.2"><H4>2.2.1.2 Extensibility to New Types</H4></A><P>Another advantage of the standard iostreams is that user-defined types can be made to fit in seamlessly. Consider a type <SAMP>Pair</SAMP> that we want to print:</P><PRE>struct Pair { int x; string y; }</PRE><P>All we need to do is overload <SAMP>operator<<()</SAMP> for this new type <SAMP>Pair,</SAMP> and we can output pairs this way:</P><PRE>Pair p(5, "May");cout << p;</PRE><P>The corresponding <SAMP>operator<<()</SAMP> can be implemented as:</P><PRE>basic_ostream<char>& operator<<(basic_ostream<char>& o, const Pair& p){ return o << p.x << ' ' << p.y; }</PRE><A NAME="2.2.2"><H3>2.2.2 How Do the Standard Iostreams Work?</H3></A><P>The main purpose of the standard iostreams is to serve as a tool for input and output of text. Generally, input and output are the transfer of data between a program and any kind of external device, as illustrated in Figure 1 below:</P><H4>Figure 1. Data transfer supported by iostreams</H4><BR><IMG SRC="images/image18.gif"><P>The internal representation of such data is meant to be convenient for data processing in a program. On the other hand, the external representation can vary quite a bit: it might be a display in human-readable form, or a portable data exchange format. The intent of a representation, such as conserving space for storage, can also influence the representation.</P><P>T<I>ext I/O</I> involves the external representation of a sequence of characters; every other case involves <I>binary I/O</I>. Traditionally, iostreams are used for text processing. Such text processing through iostreams involves two processes: <I>formatting</I> and <I>code conversion</I>.</P><P><I>Formatting</I> is the transformation from a byte sequence representing internal data into a human-readable character sequence; for example, from a floating point number, or an integer value held in a variable, into a sequence of digits. Figure 2 below illustrates the formatting process:</P><H4>Figure 2. Formatting program data</H4><BR><IMG SRC="images/image19.gif"><P><I>Code conversion</I> is the process of translating one character representation into another; for example, from wide characters held internally to a sequence of multibyte characters for external use. Wide characters are all the same size, and thus are convenient for internal data processing. Multibyte characters have different sizes and are stored more compactly. They are typically used for data transfer, or for storage on external devices such as files. Figure 3 below illustrates the conversion process:</P><H4>Figure 3. Code conversion between multibytes and wide characters</H4><BR><IMG SRC="images/image20.gif"><A NAME="2.2.2.1"><H4>2.2.2.1 The Iostream Layers</H4></A><P>The iostreams facility has two layers: one that handles formatting, and another that handles code conversion and transport of characters to and from the external device. The layers communicate through a buffer, as illustrated in Figure 4 below:</P><H4>Figure 4. The iostreams layers</H4><BR><IMG SRC="images/image21.gif"><P>Let's take a look at the function of each layer in more detail:</P><UL><LI><P><B>The Formatting Layer</B>. Here the transformation between a program's internal data representation and a readable representation as a character sequence takes place. This formatting and parsing may involve, among other things:</P><UL><LI><P>Precision and notation of floating point numbers;</P></LI><LI><P>Hexadecimal, octal, or decimal representation of integers;</P></LI><LI><P>Skipping of white space in the input;</P></LI><LI><P>Field width for output;</P></LI><LI><P>Adapting of number formatting to local conventions.</P></LI></UL></LI><LI><P><B>The Transport Layer</B>. This layer is responsible for producing and consuming characters. It encapsulates knowledge about the properties of a specific external device. Among other things, this involves:</P><UL><LI><P>Block-wise output to files through system calls;</P></LI><LI><P>Code conversion to multibyte encodings.</P></LI></UL><P>To reduce the number of accesses to the external device, a buffer is used. For output, the formatting layer sends sequences of characters to the transport layer, which stores them in a <I>stream buffer</I>. The actual transport to the external device happens only when the buffer is full. For input, the transport layer reads from the external device and fills the buffer. The formatting layer receives characters from the buffer. When the buffer is empty, the transport layer is responsible for refilling it.</P></LI><LI><P><B>Locales</B>. Both the formatting and the transport layers use the stream's locale. (See the section on internationalization and locales.) The formatting layer delegates the handling of numeric entities to the locale's numeric facets. The transport layer uses the locale's code conversion facet for character-wise transformation between the buffer content and characters transported to and from the external device. Figure 5 below shows how locales are used with iostreams:</P><H4>Figure 5. Use of locales in iostreams</H4><BR><IMG SRC="images/image22.gif"></LI></UL><A NAME="2.2.2.2"><H4>2.2.2.2 File and In-Memory I/O</H4></A><P>Iostreams support two kinds of I/O: <I>file I/O</I> and <I>in-memory I/O</I>.</P><P>File I/O involves the transfer of data to and from an external device. The device need not necessarily be a file in the usual sense of the word. It could just as well be a communication channel, or another construct that conforms to the file abstraction.</P><P>In contrast, in-memory I/O involves no external device. Thus code conversion and transport are not necessary; only formatting is performed. The result of such formatting is maintained in memory, and can be retrieved in the form of a character string.</P><A NAME="2.2.3"><H3>2.2.3 How Do the Standard Iostreams Help Solve Problems?</H3></A><P>There are many situations in which iostreams are useful:</P><UL><LI><P><B>File I/O</B>. Iostreams can still be used for input and output to files, although file I/O has lost some of it former importance. In the past, alpha-numeric user-interfaces were often built using file input/output to the standard input and output channels. Today almost all applications have graphical user interfaces.</P><P>Nevertheless, iostreams are still useful for input and output to files other than the standard input and output channels, and to all other kinds of external media that fit into the file abstraction. For example, the Rogue Wave class library for network communications programming, <I>Net.h++</I>, uses iostreams for input and output to various kinds of communication streams like sockets and pipes.</P></LI><LI><P><B>In-Memory I/O.</B> Iostreams can perform in-memory formatting and parsing. Even with a graphical user interface, you have to format the text you want to display. The standard iostreams offer internationalized in-memory I/O, which is a great help for text processing tasks like formatting. The formatting of numeric values, for example, depends on cultural conventions. The formatting layer uses a locale's numeric facets to adapt its formatting and parsing to cultural conventions.</P></LI><LI><P><B>Internationalized Text Processing. </B>This function is actively supported by iostreams.</P><P>Iostreams use locales. As locales are extensible, any kind of facet can be carried by a locale, and thus used by a stream. By default, iostreams use only the numeric and the code conversion facets of a locale. However, date , time, and monetary facets are available in the Standard C++ Library. Other cultural dependencies can be encapsulated in unique facets and made accessible to a stream. You can easily internationalize your use of iostreams to meet your needs.</P></LI><LI><P><B>Binary I/O.</B> The traditional iostreams suffer from a number of limitations. The biggest is the lack of conversion abilities: if you insert a <SAMP>double</SAMP> into a stream, for example, you do not know what format will be used to represent this <SAMP>double</SAMP> on the external device. There is no portable way to insert it as binary.</P><P>Standard iostreams are by far more flexible. The code conversion performed on transfer of internal data to external devices can be customized: the transport layer delegates the task of converting to a code conversion facet. To provide a stream with a suitable code conversion facet for binary output, you can insert a <SAMP>double</SAMP> into a file stream in a portable binary data exchange format. No such code conversion facets are provided by the Standard Library, however, and implementing such a facet is not trivial. As an alternative, you might consider implementing an entire stream buffer layer that can handle binary I/O.</P></LI><LI><P><B>Extending Iostreams.</B> In a way, you can think of iostreams as a framework that can be extended and customized. You can add input and output operators for user-defined types, or create your own formatting elements, the manipulators. You can specialize entire streams, usually in conjunction with specialized stream buffers. You can provide different locales to represent different cultural conventions, or to contain special purpose facets. You can instantiate iostreams classes for new character types, other than <SAMP>char</SAMP> or <SAMP>wchar_t</SAMP>.</P></LI></UL><A NAME="2.2.4"><H3>2.2.4 The Internal Structure of the Iostreams Layers</H3></A><P>As explained earlier, iostreams have two layers, one for formatting, and another for code conversion and transport of characters to and from the external device. For convenience, let's repeat here in Figure 6 the illustration of the iostreams layers given in Figure 4 of <A HREF="arc_7049.htm#2.2.2">Section 2.2.2</A>:</P><H4>Figure 6. The iostreams layers</H4><BR><IMG SRC="images/image23.gif">
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -