⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 inm_4073.htm

📁 C++标准库 C++标准库 C++标准库 C++标准库
💻 HTM
字号:
<HTML><HEAD><TITLE>2.6 In-Memory Input/Output</TITLE></HEAD><BODY><A HREF="ug2.htm"><IMG SRC="images/banner.gif"></A><BR><A HREF="fil_2309.htm"><IMG SRC="images/prev.gif"></A><A HREF="booktoc2.htm"><IMG SRC="images/toc.gif"></A><A HREF="inp_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.6 In-Memory Input/Output</H2><P>The iostreams facility supports not only input and output to external devices like files.  It also allows in-memory parsing and formatting.  Source and sink of the characters read or written becomes a string held somewhere in memory.  You use in-memory I/O if the information to be read is already available in the form of a string, or if the formatted result will be processed as a string.  For example, to interpret the contents of the string <SAMP>argv[1]</SAMP> as an integer value, the code might look like this:</P><PRE>int i;if (istringstream(argv[1]) >> i)                             //1   // use the value of i</PRE><TABLE CELLPADDING="3"><TR VALIGN="top"><TD>//1</TD><TD>The parameter of the input string stream constructor is a string; here a character array, namely <SAMP>argv[1]</SAMP>, is provided as an argument and is implicitly converted to a string. From this newly constructed input string stream, which contains <SAMP>argv[1],</SAMP> an integer value is extracted.</TD></TR></TABLE><P>The inverse operation, taking a value and converting it to characters that are stored in a string, might look like this:</P><PRE>struct date {  int day,month,year;} today = {8,4,1996};ostringstream ostr;                                           //1ostr &#60;&#60; today.month &#60;&#60; '-' &#60;&#60; today.day &#60;&#60;'-' &#60;&#60; today.year;  //2if (ostr)   display(ostr.str());                                       //3</PRE><TABLE CELLPADDING="3"><TR VALIGN="top"><TD>//1</TD><TD>An output string stream is allocated.</TD></TR><TR VALIGN="top"><TD>//2</TD><TD>Values are inserted into the output string stream. </TD></TR><TR VALIGN="top"><TD>//3</TD><TD>The result of the formatting can be retrieved in the form of a string, which is returned by <SAMP>str()</SAMP>.</TD></TR></TABLE><P>As with file streams, there are three class templates that implement string streams:  <B><I><B><I>basic_istringstream &#60;charT,traits,Allocator></I></B></B></I>, <B><I><B><I>basic_ostringstream &#60;charT,traits,Allocator></I></B></B></I>, and<B><I><B><I> basic_stringstream &#60;charT,traits,Allocator></I></B></B></I>.  These are derived from the stream base classes, <B><I><B><I>basic_istream &#60;charT, traits>, basic_ostream &#60;charT, traits></I></B></B></I>,  and <B><I><B><I>basic_iostream &#60;charT, traits></I></B></B></I>. Therefore they inherit all the functions for formatted input and output described in <A HREF="for_5394.htm">Section 2.3</A>, as well as the stream state.  They also have functions for setting and retrieving the string that serves as source or sink, and constructors that allow you to set the string before construction time.  For convenience, there are the regular typedefs <SAMP>istringstream</SAMP>, <SAMP>ostringstream</SAMP>, and <SAMP>stringstream,</SAMP> with<SAMP> wistringstream</SAMP>, <SAMP>wostringstream</SAMP>, and <SAMP>wstringstream</SAMP> for the respective tiny and wide character string streams.</P><P>The buffering is done through a specialized stream buffer class, <B><I><B><I>basic_stringbuf &#60;charT,traits,Allocator></I></B></B></I>. </P><A NAME="2.6.1"><H3>2.6.1 The Internal Buffer</H3></A><P>String streams can take a string, provided either as an argument to the constructor, or set later through the <SAMP>str(const basic_string &#60;charT, traits, Allocator>&#38;)</SAMP> function.  This string is copied into an internal buffer, and serves as source or sink of characters to subsequent insertions or extractions.  Each time the string is retrieved through the <SAMP>str()</SAMP> function, a copy of the internal buffer is created and returned. </P><P>Output string streams are <I>dynamic</I>.  The internal buffer is allocated once an output string stream is constructed.  The buffer is automatically extended during insertion each time the internal buffer is full.</FN><P>Input string streams are always <I>static</I>.  You can extract as many items as are available in the string you provided the string stream.</P><A NAME="2.6.2"><H3>2.6.2 The Open Modes</H3></A><P>The only open modes that have an effect on string streams are <SAMP>in</SAMP>, <SAMP>out</SAMP>, <SAMP>ate</SAMP>, and <SAMP>app</SAMP>.  They have more or less the same meaning that they have with file streams (see <A HREF="fil_2309.htm#2.5.4">section 2.5.4</A>).  </P><P>The <SAMP>binary</SAMP> open mode is irrelevant, because there is no conversion to and from the dependent file format of the operating system.  The <SAMP>trunc</SAMP> open mode is simply ignored.</P><HR><A HREF="fil_2309.htm"><IMG SRC="images/prev.gif"></A> <A HREF="booktoc2.htm"><IMG SRC="images/toc.gif"></A><A HREF="inp_5394.htm"><IMG SRC="images/next.gif"></A><P>&copy;Copyright 1996, Rogue Wave Software, Inc.</P></BODY></HTML>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -