1 |
424 |
jeremybenn |
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
|
2 |
|
|
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
|
3 |
|
|
<html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><title>Buffering</title><meta name="generator" content="DocBook XSL Stylesheets V1.75.2" /><meta name="keywords" content=" ISO C++ , library " /><link rel="home" href="../spine.html" title="The GNU C++ Library Documentation" /><link rel="up" href="streambufs.html" title="Chapter 25. Stream Buffers" /><link rel="prev" href="streambufs.html" title="Chapter 25. Stream Buffers" /><link rel="next" href="stringstreams.html" title="Chapter 26. Memory Based Streams" /></head><body><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="3" align="center">Buffering</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="streambufs.html">Prev</a> </td><th width="60%" align="center">Chapter 25. Stream Buffers</th><td width="20%" align="right"> <a accesskey="n" href="stringstreams.html">Next</a></td></tr></table><hr /></div><div class="sect1" title="Buffering"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a id="io.streambuf.buffering"></a>Buffering</h2></div></div></div><p>First, are you sure that you understand buffering? Particularly
|
4 |
|
|
the fact that C++ may not, in fact, have anything to do with it?
|
5 |
|
|
</p><p>The rules for buffering can be a little odd, but they aren't any
|
6 |
|
|
different from those of C. (Maybe that's why they can be a bit
|
7 |
|
|
odd.) Many people think that writing a newline to an output
|
8 |
|
|
stream automatically flushes the output buffer. This is true only
|
9 |
|
|
when the output stream is, in fact, a terminal and not a file
|
10 |
|
|
or some other device -- and <span class="emphasis"><em>that</em></span> may not even be true
|
11 |
|
|
since C++ says nothing about files nor terminals. All of that is
|
12 |
|
|
system-dependent. (The "newline-buffer-flushing only occurring
|
13 |
|
|
on terminals" thing is mostly true on Unix systems, though.)
|
14 |
|
|
</p><p>Some people also believe that sending <code class="code">endl</code> down an
|
15 |
|
|
output stream only writes a newline. This is incorrect; after a
|
16 |
|
|
newline is written, the buffer is also flushed. Perhaps this
|
17 |
|
|
is the effect you want when writing to a screen -- get the text
|
18 |
|
|
out as soon as possible, etc -- but the buffering is largely
|
19 |
|
|
wasted when doing this to a file:
|
20 |
|
|
</p><pre class="programlisting">
|
21 |
|
|
output << "a line of text" << endl;
|
22 |
|
|
output << some_data_variable << endl;
|
23 |
|
|
output << "another line of text" << endl; </pre><p>The proper thing to do in this case to just write the data out
|
24 |
|
|
and let the libraries and the system worry about the buffering.
|
25 |
|
|
If you need a newline, just write a newline:
|
26 |
|
|
</p><pre class="programlisting">
|
27 |
|
|
output << "a line of text\n"
|
28 |
|
|
<< some_data_variable << '\n'
|
29 |
|
|
<< "another line of text\n"; </pre><p>I have also joined the output statements into a single statement.
|
30 |
|
|
You could make the code prettier by moving the single newline to
|
31 |
|
|
the start of the quoted text on the last line, for example.
|
32 |
|
|
</p><p>If you do need to flush the buffer above, you can send an
|
33 |
|
|
<code class="code">endl</code> if you also need a newline, or just flush the buffer
|
34 |
|
|
yourself:
|
35 |
|
|
</p><pre class="programlisting">
|
36 |
|
|
output << ...... << flush; // can use std::flush manipulator
|
37 |
|
|
output.flush(); // or call a member fn </pre><p>On the other hand, there are times when writing to a file should
|
38 |
|
|
be like writing to standard error; no buffering should be done
|
39 |
|
|
because the data needs to appear quickly (a prime example is a
|
40 |
|
|
log file for security-related information). The way to do this is
|
41 |
|
|
just to turn off the buffering <span class="emphasis"><em>before any I/O operations at
|
42 |
|
|
all</em></span> have been done (note that opening counts as an I/O operation):
|
43 |
|
|
</p><pre class="programlisting">
|
44 |
|
|
std::ofstream os;
|
45 |
|
|
std::ifstream is;
|
46 |
|
|
int i;
|
47 |
|
|
|
48 |
|
|
os.rdbuf()->pubsetbuf(0,0);
|
49 |
|
|
is.rdbuf()->pubsetbuf(0,0);
|
50 |
|
|
|
51 |
|
|
os.open("/foo/bar/baz");
|
52 |
|
|
is.open("/qux/quux/quuux");
|
53 |
|
|
...
|
54 |
|
|
os << "this data is written immediately\n";
|
55 |
|
|
is >> i; // and this will probably cause a disk read </pre><p>Since all aspects of buffering are handled by a streambuf-derived
|
56 |
|
|
member, it is necessary to get at that member with <code class="code">rdbuf()</code>.
|
57 |
|
|
Then the public version of <code class="code">setbuf</code> can be called. The
|
58 |
|
|
arguments are the same as those for the Standard C I/O Library
|
59 |
|
|
function (a buffer area followed by its size).
|
60 |
|
|
</p><p>A great deal of this is implementation-dependent. For example,
|
61 |
|
|
<code class="code">streambuf</code> does not specify any actions for its own
|
62 |
|
|
<code class="code">setbuf()</code>-ish functions; the classes derived from
|
63 |
|
|
<code class="code">streambuf</code> each define behavior that "makes
|
64 |
|
|
sense" for that class: an argument of (0,0) turns off buffering
|
65 |
|
|
for <code class="code">filebuf</code> but does nothing at all for its siblings
|
66 |
|
|
<code class="code">stringbuf</code> and <code class="code">strstreambuf</code>, and specifying
|
67 |
|
|
anything other than (0,0) has varying effects.
|
68 |
|
|
User-defined classes derived from <code class="code">streambuf</code> can
|
69 |
|
|
do whatever they want. (For <code class="code">filebuf</code> and arguments for
|
70 |
|
|
<code class="code">(p,s)</code> other than zeros, libstdc++ does what you'd expect:
|
71 |
|
|
the first <code class="code">s</code> bytes of <code class="code">p</code> are used as a buffer,
|
72 |
|
|
which you must allocate and deallocate.)
|
73 |
|
|
</p><p>A last reminder: there are usually more buffers involved than
|
74 |
|
|
just those at the language/library level. Kernel buffers, disk
|
75 |
|
|
buffers, and the like will also have an effect. Inspecting and
|
76 |
|
|
changing those are system-dependent.
|
77 |
|
|
</p></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="streambufs.html">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="stringstreams.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">Chapter 25. Stream Buffers </td><td width="20%" align="center"><a accesskey="h" href="../spine.html">Home</a></td><td width="40%" align="right" valign="top"> Chapter 26. Memory Based Streams</td></tr></table></div></body></html>
|