*: Regenerate.
[gcc.git] / libstdc++-v3 / doc / html / manual / stringstreams.html
1 <html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>Memory Based Streams</title><meta name="generator" content="DocBook XSL-NS Stylesheets V1.76.1"><meta name="keywords" content="
2 ISO C++
3 ,
4 library
5 "><meta name="keywords" content="
6 ISO C++
7 ,
8 runtime
9 ,
10 library
11 "><link rel="home" href="../index.html" title="The GNU C++ Library"><link rel="up" href="io.html" title="Chapter 13.  Input and Output"><link rel="prev" href="streambufs.html" title="Stream Buffers"><link rel="next" href="fstreams.html" title="File Based Streams"></head><body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="3" align="center">Memory Based Streams</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="streambufs.html">Prev</a> </td><th width="60%" align="center">Chapter 13
12 Input and Output
13
14 </th><td width="20%" align="right"> <a accesskey="n" href="fstreams.html">Next</a></td></tr></table><hr></div><div class="section" title="Memory Based Streams"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="std.io.memstreams"></a>Memory Based Streams</h2></div></div></div><div class="section" title="Compatibility With strstream"><div class="titlepage"><div><div><h3 class="title"><a name="std.io.memstreams.compat"></a>Compatibility With strstream</h3></div></div></div><p>
15 </p><p>Stringstreams (defined in the header <code class="code">&lt;sstream&gt;</code>)
16 are in this author's opinion one of the coolest things since
17 sliced time. An example of their use is in the Received Wisdom
18 section for Sect1 21 (Strings),
19 <a class="link" href="strings.html#strings.string.Cstring" title="CString (MFC)"> describing how to
20 format strings</a>.
21 </p><p>The quick definition is: they are siblings of ifstream and ofstream,
22 and they do for <code class="code">std::string</code> what their siblings do for
23 files. All that work you put into writing <code class="code">&lt;&lt;</code> and
24 <code class="code">&gt;&gt;</code> functions for your classes now pays off
25 <span class="emphasis"><em>again!</em></span> Need to format a string before passing the string
26 to a function? Send your stuff via <code class="code">&lt;&lt;</code> to an
27 ostringstream. You've read a string as input and need to parse it?
28 Initialize an istringstream with that string, and then pull pieces
29 out of it with <code class="code">&gt;&gt;</code>. Have a stringstream and need to
30 get a copy of the string inside? Just call the <code class="code">str()</code>
31 member function.
32 </p><p>This only works if you've written your
33 <code class="code">&lt;&lt;</code>/<code class="code">&gt;&gt;</code> functions correctly, though,
34 and correctly means that they take istreams and ostreams as
35 parameters, not i<span class="emphasis"><em>f</em></span>streams and o<span class="emphasis"><em>f</em></span>streams. If they
36 take the latter, then your I/O operators will work fine with
37 file streams, but with nothing else -- including stringstreams.
38 </p><p>If you are a user of the strstream classes, you need to update
39 your code. You don't have to explicitly append <code class="code">ends</code> to
40 terminate the C-style character array, you don't have to mess with
41 "freezing" functions, and you don't have to manage the
42 memory yourself. The strstreams have been officially deprecated,
43 which means that 1) future revisions of the C++ Standard won't
44 support them, and 2) if you use them, people will laugh at you.
45 </p></div></div><div class="navfooter"><hr><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="streambufs.html">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="io.html">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="fstreams.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">Stream Buffers </td><td width="20%" align="center"><a accesskey="h" href="../index.html">Home</a></td><td width="40%" align="right" valign="top"> File Based Streams</td></tr></table></div></body></html>