xref: /netbsd-src/external/gpl3/gcc.old/dist/libstdc++-v3/doc/html/manual/stringstreams.html (revision 36ac495d2b3ea2b9d96377b2143ebfedac224b92)
1*36ac495dSmrg<?xml version="1.0" encoding="UTF-8" standalone="no"?>
2*36ac495dSmrg<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><title>Memory Based Streams</title><meta name="generator" content="DocBook XSL Stylesheets Vsnapshot" /><meta name="keywords" content="ISO C++, library" /><meta name="keywords" content="ISO C++, runtime, library" /><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><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. 
3*36ac495dSmrg  Input and Output
4*36ac495dSmrg
5*36ac495dSmrg</th><td width="20%" align="right"> <a accesskey="n" href="fstreams.html">Next</a></td></tr></table><hr /></div><div class="section"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a id="std.io.memstreams"></a>Memory Based Streams</h2></div></div></div><div class="section"><div class="titlepage"><div><div><h3 class="title"><a id="std.io.memstreams.compat"></a>Compatibility With strstream</h3></div></div></div><p>
6*36ac495dSmrg    </p><p>Stringstreams (defined in the header <code class="code">&lt;sstream&gt;</code>)
7*36ac495dSmrg      are in this author's opinion one of the coolest things since
8*36ac495dSmrg      sliced time.  An example of their use is in the Received Wisdom
9*36ac495dSmrg      section for Sect1 21 (Strings),
10*36ac495dSmrg      <a class="link" href="strings.html#strings.string.Cstring" title="CString (MFC)"> describing how to
11*36ac495dSmrg      format strings</a>.
12*36ac495dSmrg   </p><p>The quick definition is:  they are siblings of ifstream and ofstream,
13*36ac495dSmrg      and they do for <code class="code">std::string</code> what their siblings do for
14*36ac495dSmrg      files.  All that work you put into writing <code class="code">&lt;&lt;</code> and
15*36ac495dSmrg      <code class="code">&gt;&gt;</code> functions for your classes now pays off
16*36ac495dSmrg      <span class="emphasis"><em>again!</em></span>  Need to format a string before passing the string
17*36ac495dSmrg      to a function?  Send your stuff via <code class="code">&lt;&lt;</code> to an
18*36ac495dSmrg      ostringstream.  You've read a string as input and need to parse it?
19*36ac495dSmrg      Initialize an istringstream with that string, and then pull pieces
20*36ac495dSmrg      out of it with <code class="code">&gt;&gt;</code>.  Have a stringstream and need to
21*36ac495dSmrg      get a copy of the string inside?  Just call the <code class="code">str()</code>
22*36ac495dSmrg      member function.
23*36ac495dSmrg   </p><p>This only works if you've written your
24*36ac495dSmrg      <code class="code">&lt;&lt;</code>/<code class="code">&gt;&gt;</code> functions correctly, though,
25*36ac495dSmrg      and correctly means that they take istreams and ostreams as
26*36ac495dSmrg      parameters, not i<span class="emphasis"><em>f</em></span>streams and o<span class="emphasis"><em>f</em></span>streams.  If they
27*36ac495dSmrg      take the latter, then your I/O operators will work fine with
28*36ac495dSmrg      file streams, but with nothing else -- including stringstreams.
29*36ac495dSmrg   </p><p>If you are a user of the strstream classes, you need to update
30*36ac495dSmrg      your code.  You don't have to explicitly append <code class="code">ends</code> to
31*36ac495dSmrg      terminate the C-style character array, you don't have to mess with
32*36ac495dSmrg      "freezing" functions, and you don't have to manage the
33*36ac495dSmrg      memory yourself.  The strstreams have been officially deprecated,
34*36ac495dSmrg      which means that 1) future revisions of the C++ Standard won't
35*36ac495dSmrg      support them, and 2) if you use them, people will laugh at you.
36*36ac495dSmrg   </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>