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