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