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