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>Stream Buffers</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="io.html" title="Chapter 13. Input and Output" /><link rel="next" href="stringstreams.html" title="Memory Based Streams" /></head><body><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="3" align="center">Stream Buffers</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="io.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="stringstreams.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.streambufs"></a>Stream Buffers</h2></div></div></div><div class="section"><div class="titlepage"><div><div><h3 class="title"><a id="io.streambuf.derived"></a>Derived streambuf Classes</h3></div></div></div><p> 6*36ac495dSmrg </p><p>Creating your own stream buffers for I/O can be remarkably easy. 7*36ac495dSmrg If you are interested in doing so, we highly recommend two very 8*36ac495dSmrg excellent books: 9*36ac495dSmrg <a class="link" href="http://www.angelikalanger.com/iostreams.html" target="_top">Standard C++ 10*36ac495dSmrg IOStreams and Locales</a> by Langer and Kreft, ISBN 0-201-18395-1, and 11*36ac495dSmrg <a class="link" href="http://www.josuttis.com/libbook/" target="_top">The C++ Standard Library</a> 12*36ac495dSmrg by Nicolai Josuttis, ISBN 0-201-37926-0. Both are published by 13*36ac495dSmrg Addison-Wesley, who isn't paying us a cent for saying that, honest. 14*36ac495dSmrg </p><p>Here is a simple example, io/outbuf1, from the Josuttis text. It 15*36ac495dSmrg transforms everything sent through it to uppercase. This version 16*36ac495dSmrg assumes many things about the nature of the character type being 17*36ac495dSmrg used (for more information, read the books or the newsgroups): 18*36ac495dSmrg </p><pre class="programlisting"> 19*36ac495dSmrg #include <iostream> 20*36ac495dSmrg #include <streambuf> 21*36ac495dSmrg #include <locale> 22*36ac495dSmrg #include <cstdio> 23*36ac495dSmrg 24*36ac495dSmrg class outbuf : public std::streambuf 25*36ac495dSmrg { 26*36ac495dSmrg protected: 27*36ac495dSmrg /* central output function 28*36ac495dSmrg * - print characters in uppercase mode 29*36ac495dSmrg */ 30*36ac495dSmrg virtual int_type overflow (int_type c) { 31*36ac495dSmrg if (c != EOF) { 32*36ac495dSmrg // convert lowercase to uppercase 33*36ac495dSmrg c = std::toupper(static_cast<char>(c),getloc()); 34*36ac495dSmrg 35*36ac495dSmrg // and write the character to the standard output 36*36ac495dSmrg if (putchar(c) == EOF) { 37*36ac495dSmrg return EOF; 38*36ac495dSmrg } 39*36ac495dSmrg } 40*36ac495dSmrg return c; 41*36ac495dSmrg } 42*36ac495dSmrg }; 43*36ac495dSmrg 44*36ac495dSmrg int main() 45*36ac495dSmrg { 46*36ac495dSmrg // create special output buffer 47*36ac495dSmrg outbuf ob; 48*36ac495dSmrg // initialize output stream with that output buffer 49*36ac495dSmrg std::ostream out(&ob); 50*36ac495dSmrg 51*36ac495dSmrg out << "31 hexadecimal: " 52*36ac495dSmrg << std::hex << 31 << std::endl; 53*36ac495dSmrg return 0; 54*36ac495dSmrg } 55*36ac495dSmrg </pre><p>Try it yourself! More examples can be found in 3.1.x code, in 56*36ac495dSmrg <code class="filename">include/ext/*_filebuf.h</code>, and in the article 57*36ac495dSmrg <a class="link" href="http://gabisoft.free.fr/articles/fltrsbf1.html" target="_top">Filtering 58*36ac495dSmrg Streambufs</a> 59*36ac495dSmrg by James Kanze. 60*36ac495dSmrg </p></div><div class="section"><div class="titlepage"><div><div><h3 class="title"><a id="io.streambuf.buffering"></a>Buffering</h3></div></div></div><p>First, are you sure that you understand buffering? Particularly 61*36ac495dSmrg the fact that C++ may not, in fact, have anything to do with it? 62*36ac495dSmrg </p><p>The rules for buffering can be a little odd, but they aren't any 63*36ac495dSmrg different from those of C. (Maybe that's why they can be a bit 64*36ac495dSmrg odd.) Many people think that writing a newline to an output 65*36ac495dSmrg stream automatically flushes the output buffer. This is true only 66*36ac495dSmrg when the output stream is, in fact, a terminal and not a file 67*36ac495dSmrg or some other device -- and <span class="emphasis"><em>that</em></span> may not even be true 68*36ac495dSmrg since C++ says nothing about files nor terminals. All of that is 69*36ac495dSmrg system-dependent. (The "newline-buffer-flushing only occurring 70*36ac495dSmrg on terminals" thing is mostly true on Unix systems, though.) 71*36ac495dSmrg </p><p>Some people also believe that sending <code class="code">endl</code> down an 72*36ac495dSmrg output stream only writes a newline. This is incorrect; after a 73*36ac495dSmrg newline is written, the buffer is also flushed. Perhaps this 74*36ac495dSmrg is the effect you want when writing to a screen -- get the text 75*36ac495dSmrg out as soon as possible, etc -- but the buffering is largely 76*36ac495dSmrg wasted when doing this to a file: 77*36ac495dSmrg </p><pre class="programlisting"> 78*36ac495dSmrg output << "a line of text" << endl; 79*36ac495dSmrg output << some_data_variable << endl; 80*36ac495dSmrg output << "another line of text" << endl; </pre><p>The proper thing to do in this case to just write the data out 81*36ac495dSmrg and let the libraries and the system worry about the buffering. 82*36ac495dSmrg If you need a newline, just write a newline: 83*36ac495dSmrg </p><pre class="programlisting"> 84*36ac495dSmrg output << "a line of text\n" 85*36ac495dSmrg << some_data_variable << '\n' 86*36ac495dSmrg << "another line of text\n"; </pre><p>I have also joined the output statements into a single statement. 87*36ac495dSmrg You could make the code prettier by moving the single newline to 88*36ac495dSmrg the start of the quoted text on the last line, for example. 89*36ac495dSmrg </p><p>If you do need to flush the buffer above, you can send an 90*36ac495dSmrg <code class="code">endl</code> if you also need a newline, or just flush the buffer 91*36ac495dSmrg yourself: 92*36ac495dSmrg </p><pre class="programlisting"> 93*36ac495dSmrg output << ...... << flush; // can use std::flush manipulator 94*36ac495dSmrg output.flush(); // or call a member fn </pre><p>On the other hand, there are times when writing to a file should 95*36ac495dSmrg be like writing to standard error; no buffering should be done 96*36ac495dSmrg because the data needs to appear quickly (a prime example is a 97*36ac495dSmrg log file for security-related information). The way to do this is 98*36ac495dSmrg just to turn off the buffering <span class="emphasis"><em>before any I/O operations at 99*36ac495dSmrg all</em></span> have been done (note that opening counts as an I/O operation): 100*36ac495dSmrg </p><pre class="programlisting"> 101*36ac495dSmrg std::ofstream os; 102*36ac495dSmrg std::ifstream is; 103*36ac495dSmrg int i; 104*36ac495dSmrg 105*36ac495dSmrg os.rdbuf()->pubsetbuf(0,0); 106*36ac495dSmrg is.rdbuf()->pubsetbuf(0,0); 107*36ac495dSmrg 108*36ac495dSmrg os.open("/foo/bar/baz"); 109*36ac495dSmrg is.open("/qux/quux/quuux"); 110*36ac495dSmrg ... 111*36ac495dSmrg os << "this data is written immediately\n"; 112*36ac495dSmrg is >> i; // and this will probably cause a disk read </pre><p>Since all aspects of buffering are handled by a streambuf-derived 113*36ac495dSmrg member, it is necessary to get at that member with <code class="code">rdbuf()</code>. 114*36ac495dSmrg Then the public version of <code class="code">setbuf</code> can be called. The 115*36ac495dSmrg arguments are the same as those for the Standard C I/O Library 116*36ac495dSmrg function (a buffer area followed by its size). 117*36ac495dSmrg </p><p>A great deal of this is implementation-dependent. For example, 118*36ac495dSmrg <code class="code">streambuf</code> does not specify any actions for its own 119*36ac495dSmrg <code class="code">setbuf()</code>-ish functions; the classes derived from 120*36ac495dSmrg <code class="code">streambuf</code> each define behavior that "makes 121*36ac495dSmrg sense" for that class: an argument of (0,0) turns off buffering 122*36ac495dSmrg for <code class="code">filebuf</code> but does nothing at all for its siblings 123*36ac495dSmrg <code class="code">stringbuf</code> and <code class="code">strstreambuf</code>, and specifying 124*36ac495dSmrg anything other than (0,0) has varying effects. 125*36ac495dSmrg User-defined classes derived from <code class="code">streambuf</code> can 126*36ac495dSmrg do whatever they want. (For <code class="code">filebuf</code> and arguments for 127*36ac495dSmrg <code class="code">(p,s)</code> other than zeros, libstdc++ does what you'd expect: 128*36ac495dSmrg the first <code class="code">s</code> bytes of <code class="code">p</code> are used as a buffer, 129*36ac495dSmrg which you must allocate and deallocate.) 130*36ac495dSmrg </p><p>A last reminder: there are usually more buffers involved than 131*36ac495dSmrg just those at the language/library level. Kernel buffers, disk 132*36ac495dSmrg buffers, and the like will also have an effect. Inspecting and 133*36ac495dSmrg changing those are system-dependent. 134*36ac495dSmrg </p></div></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="io.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="stringstreams.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">Chapter 13. 135*36ac495dSmrg Input and Output 136*36ac495dSmrg 137*36ac495dSmrg </td><td width="20%" align="center"><a accesskey="h" href="../index.html">Home</a></td><td width="40%" align="right" valign="top"> Memory Based Streams</td></tr></table></div></body></html>