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"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><title>Interacting with C</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="fstreams.html" title="File Based Streams" /><link rel="next" href="atomics.html" title="Chapter 14. Atomics" /></head><body><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="3" align="center">Interacting with C</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="fstreams.html">Prev</a> </td><th width="60%" align="center">Chapter 13. 3 Input and Output 4 5</th><td width="20%" align="right"> <a accesskey="n" href="atomics.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.c"></a>Interacting with C</h2></div></div></div><div class="section"><div class="titlepage"><div><div><h3 class="title"><a id="std.io.c.FILE"></a>Using FILE* and file descriptors</h3></div></div></div><p> 6 See the <a class="link" href="ext_io.html" title="Chapter 27. Input and Output">extensions</a> for using 7 <span class="type">FILE</span> and <span class="type">file descriptors</span> with 8 <code class="classname">ofstream</code> and 9 <code class="classname">ifstream</code>. 10 </p></div><div class="section"><div class="titlepage"><div><div><h3 class="title"><a id="std.io.c.sync"></a>Performance</h3></div></div></div><p> 11 Pathetic Performance? Ditch C. 12 </p><p>It sounds like a flame on C, but it isn't. Really. Calm down. 13 I'm just saying it to get your attention. 14 </p><p>Because the C++ library includes the C library, both C-style and 15 C++-style I/O have to work at the same time. For example: 16 </p><pre class="programlisting"> 17 #include <iostream> 18 #include <cstdio> 19 20 std::cout << "Hel"; 21 std::printf ("lo, worl"); 22 std::cout << "d!\n"; 23 </pre><p>This must do what you think it does. 24 </p><p>Alert members of the audience will immediately notice that buffering 25 is going to make a hash of the output unless special steps are taken. 26 </p><p>The special steps taken by libstdc++, at least for version 3.0, 27 involve doing very little buffering for the standard streams, leaving 28 most of the buffering to the underlying C library. (This kind of 29 thing is tricky to get right.) 30 The upside is that correctness is ensured. The downside is that 31 writing through <code class="code">cout</code> can quite easily lead to awful 32 performance when the C++ I/O library is layered on top of the C I/O 33 library (as it is for 3.0 by default). Some patches have been applied 34 which improve the situation for 3.1. 35 </p><p>However, the C and C++ standard streams only need to be kept in sync 36 when both libraries' facilities are in use. If your program only uses 37 C++ I/O, then there's no need to sync with the C streams. The right 38 thing to do in this case is to call 39 </p><pre class="programlisting"> 40 #include <span class="emphasis"><em>any of the I/O headers such as ios, iostream, etc</em></span> 41 42 std::ios::sync_with_stdio(false); 43 </pre><p>You must do this before performing any I/O via the C++ stream objects. 44 Once you call this, the C++ streams will operate independently of the 45 (unused) C streams. For GCC 3.x, this means that <code class="code">cout</code> and 46 company will become fully buffered on their own. 47 </p><p>Note, by the way, that the synchronization requirement only applies to 48 the standard streams (<code class="code">cin</code>, <code class="code">cout</code>, 49 <code class="code">cerr</code>, 50 <code class="code">clog</code>, and their wide-character counterparts). File stream 51 objects that you declare yourself have no such requirement and are fully 52 buffered. 53 </p></div></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="fstreams.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="atomics.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">File Based Streams </td><td width="20%" align="center"><a accesskey="h" href="../index.html">Home</a></td><td width="40%" align="right" valign="top"> Chapter 14. 54 Atomics 55 56</td></tr></table></div></body></html>