aboutsummaryrefslogtreecommitdiff
path: root/libstdc++-v3/doc/html/manual/io_and_c.html
blob: 14eaf486a5bb32c6ea4ccec0c4897453da2e76f3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!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-NS Stylesheets V1.77.1" /><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. 
  Input and Output
  
</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>
      See the <a class="link" href="ext_io.html" title="Chapter 28. Input and Output">extensions</a> for using
      <span class="type">FILE</span> and <span class="type">file descriptors</span> with
      <code class="classname">ofstream</code> and
      <code class="classname">ifstream</code>.
    </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>
      Pathetic Performance? Ditch C.
    </p><p>It sounds like a flame on C, but it isn't.  Really.  Calm down.
      I'm just saying it to get your attention.
   </p><p>Because the C++ library includes the C library, both C-style and
      C++-style I/O have to work at the same time.  For example:
   </p><pre class="programlisting">
     #include &lt;iostream&gt;
     #include &lt;cstdio&gt;

     std::cout &lt;&lt; "Hel";
     std::printf ("lo, worl");
     std::cout &lt;&lt; "d!\n";
   </pre><p>This must do what you think it does.
   </p><p>Alert members of the audience will immediately notice that buffering
      is going to make a hash of the output unless special steps are taken.
   </p><p>The special steps taken by libstdc++, at least for version 3.0,
      involve doing very little buffering for the standard streams, leaving
      most of the buffering to the underlying C library.  (This kind of
      thing is tricky to get right.)
      The upside is that correctness is ensured.  The downside is that
      writing through <code class="code">cout</code> can quite easily lead to awful
      performance when the C++ I/O library is layered on top of the C I/O
      library (as it is for 3.0 by default).  Some patches have been applied
      which improve the situation for 3.1.
   </p><p>However, the C and C++ standard streams only need to be kept in sync
      when both libraries' facilities are in use.  If your program only uses
      C++ I/O, then there's no need to sync with the C streams.  The right
      thing to do in this case is to call
   </p><pre class="programlisting">
     #include <span class="emphasis"><em>any of the I/O headers such as ios, iostream, etc</em></span>

     std::ios::sync_with_stdio(false);
   </pre><p>You must do this before performing any I/O via the C++ stream objects.
      Once you call this, the C++ streams will operate independently of the
      (unused) C streams.  For GCC 3.x, this means that <code class="code">cout</code> and
      company will become fully buffered on their own.
   </p><p>Note, by the way, that the synchronization requirement only applies to
      the standard streams (<code class="code">cin</code>, <code class="code">cout</code>,
      <code class="code">cerr</code>,
      <code class="code">clog</code>, and their wide-character counterchapters).  File stream
      objects that you declare yourself have no such requirement and are fully
      buffered.
   </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. 
  Atomics
  
</td></tr></table></div></body></html>