streambufs.html revision 1.1.1.1
1<?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>Chapter��25.��Stream Buffers</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="io.html" title="Part��XI.�� Input and Output" /><link rel="prev" href="iostream_objects.html" title="Chapter��24.��Iostream Objects" /><link rel="next" href="bk01pt11ch25s02.html" title="Buffering" /></head><body><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="3" align="center">Chapter��25.��Stream Buffers</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="iostream_objects.html">Prev</a>��</td><th width="60%" align="center">Part��XI.�� 4 Input and Output 5 6</th><td width="20%" align="right">��<a accesskey="n" href="bk01pt11ch25s02.html">Next</a></td></tr></table><hr /></div><div class="chapter" title="Chapter��25.��Stream Buffers"><div class="titlepage"><div><div><h2 class="title"><a id="manual.io.streambufs"></a>Chapter��25.��Stream Buffers</h2></div></div></div><div class="toc"><p><b>Table of Contents</b></p><dl><dt><span class="sect1"><a href="streambufs.html#io.streambuf.derived">Derived streambuf Classes</a></span></dt><dt><span class="sect1"><a href="bk01pt11ch25s02.html">Buffering</a></span></dt></dl></div><div class="sect1" title="Derived streambuf Classes"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a id="io.streambuf.derived"></a>Derived streambuf Classes</h2></div></div></div><p> 7 </p><p>Creating your own stream buffers for I/O can be remarkably easy. 8 If you are interested in doing so, we highly recommend two very 9 excellent books: 10 <a class="ulink" href="http://www.angelikalanger.com/iostreams.html" target="_top">Standard C++ 11 IOStreams and Locales</a> by Langer and Kreft, ISBN 0-201-18395-1, and 12 <a class="ulink" href="http://www.josuttis.com/libbook/" target="_top">The C++ Standard Library</a> 13 by Nicolai Josuttis, ISBN 0-201-37926-0. Both are published by 14 Addison-Wesley, who isn't paying us a cent for saying that, honest. 15 </p><p>Here is a simple example, io/outbuf1, from the Josuttis text. It 16 transforms everything sent through it to uppercase. This version 17 assumes many things about the nature of the character type being 18 used (for more information, read the books or the newsgroups): 19 </p><pre class="programlisting"> 20 #include <iostream> 21 #include <streambuf> 22 #include <locale> 23 #include <cstdio> 24 25 class outbuf : public std::streambuf 26 { 27 protected: 28 /* central output function 29 * - print characters in uppercase mode 30 */ 31 virtual int_type overflow (int_type c) { 32 if (c != EOF) { 33 // convert lowercase to uppercase 34 c = std::toupper(static_cast<char>(c),getloc()); 35 36 // and write the character to the standard output 37 if (putchar(c) == EOF) { 38 return EOF; 39 } 40 } 41 return c; 42 } 43 }; 44 45 int main() 46 { 47 // create special output buffer 48 outbuf ob; 49 // initialize output stream with that output buffer 50 std::ostream out(&ob); 51 52 out << "31 hexadecimal: " 53 << std::hex << 31 << std::endl; 54 return 0; 55 } 56 </pre><p>Try it yourself! More examples can be found in 3.1.x code, in 57 <code class="code">include/ext/*_filebuf.h</code>, and in this article by James Kanze: 58 <a class="ulink" href="http://kanze.james.neuf.fr/articles/fltrsbf1.html" target="_top">Filtering 59 Streambufs</a>. 60 </p></div></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="iostream_objects.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="bk01pt11ch25s02.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">Chapter��24.��Iostream Objects��</td><td width="20%" align="center"><a accesskey="h" href="../spine.html">Home</a></td><td width="40%" align="right" valign="top">��Buffering</td></tr></table></div></body></html> 61