xref: /netbsd-src/external/gpl3/gcc.old/dist/libstdc++-v3/doc/xml/manual/io.xml (revision bdc22b2e01993381dcefeff2bc9b56ca75a4235c)
1<chapter xmlns="http://docbook.org/ns/docbook" version="5.0"
2	 xml:id="std.io" xreflabel="Input and Output">
3<?dbhtml filename="io.html"?>
4
5<info><title>
6  Input and Output
7  <indexterm><primary>Input and Output</primary></indexterm>
8</title>
9  <keywordset>
10    <keyword>ISO C++</keyword>
11    <keyword>library</keyword>
12  </keywordset>
13</info>
14
15
16
17<!-- Sect1 01 : Iostream Objects -->
18<section xml:id="std.io.objects" xreflabel="IO Objects"><info><title>Iostream Objects</title></info>
19<?dbhtml filename="iostream_objects.html"?>
20
21
22   <para>To minimize the time you have to wait on the compiler, it's good to
23      only include the headers you really need.  Many people simply include
24      <filename class="headerfile">&lt;iostream&gt;</filename> when they don't
25      need to -- and that can <emphasis>penalize your runtime as well.</emphasis>
26      Here are some tips on which header to use
27      for which situations, starting with the simplest.
28   </para>
29   <para><emphasis><filename class="headerfile">&lt;iosfwd&gt;</filename></emphasis>
30      should be included whenever you simply need the <emphasis>name</emphasis>
31      of an I/O-related class, such as "<classname>ofstream</classname>" or
32      "<classname>basic_streambuf</classname>".
33      Like the name implies, these are forward declarations.
34      (A word to all you fellow old school programmers:
35      trying to forward declare classes like "<code>class istream;</code>"
36      won't work.
37      Look in the <filename class="headerfile">&lt;iosfwd&gt;</filename> header
38      if you'd like to know why.)  For example,
39   </para>
40   <programlisting>
41    #include &lt;iosfwd&gt;
42
43    class MyClass
44    {
45	....
46	std::ifstream&amp;   input_file;
47    };
48
49    extern std::ostream&amp; operator&lt;&lt; (std::ostream&amp;, MyClass&amp;);
50   </programlisting>
51   <para><emphasis><filename class="headerfile">&lt;ios&gt;</filename></emphasis>
52      declares the base classes for the entire I/O stream hierarchy,
53      <classname>std::ios_base</classname> and <classname>std::basic_ios&lt;charT&gt;</classname>,
54      the counting types <type>std::streamoff</type> and <type>std::streamsize</type>,
55      the file positioning type <type>std::fpos</type>,
56      and the various manipulators like <function>std::hex</function>,
57      <function>std::fixed</function>, <function>std::noshowbase</function>,
58      and so forth.
59   </para>
60   <para>The <classname>ios_base</classname> class is what holds the format
61      flags, the state flags, and the functions which change them
62      (<function>setf()</function>, <function>width()</function>,
63      <function>precision()</function>, etc).
64      You can also store extra data and register callback functions
65      through <classname>ios_base</classname>, but that has been historically
66      underused.  Anything
67      which doesn't depend on the type of characters stored is consolidated
68      here.
69   </para>
70   <para>The class template <classname>basic_ios</classname> is the highest
71      class template in the
72      hierarchy; it is the first one depending on the character type, and
73      holds all general state associated with that type:  the pointer to the
74      polymorphic stream buffer, the facet information, etc.
75   </para>
76   <para><emphasis><filename class="headerfile">&lt;streambuf&gt;</filename></emphasis>
77      declares the class template <classname>basic_streambuf</classname>, and
78      two standard instantiations, <type>streambuf</type> and
79      <type>wstreambuf</type>.  If you need to work with the vastly useful and
80      capable stream buffer classes, e.g., to create a new form of storage
81      transport, this header is the one to include.
82   </para>
83   <para><emphasis><filename class="headerfile">&lt;istream&gt;</filename></emphasis>
84       and <emphasis><filename class="headerfile">&lt;ostream&gt;</filename></emphasis>
85       are the headers to include when you are using the overloaded
86      <code>&gt;&gt;</code> and <code>&lt;&lt;</code> operators,
87      or any of the other abstract stream formatting functions.
88      For example,
89   </para>
90   <programlisting>
91    #include &lt;istream&gt;
92
93    std::ostream&amp; operator&lt;&lt; (std::ostream&amp; os, MyClass&amp; c)
94    {
95       return os &lt;&lt; c.data1() &lt;&lt; c.data2();
96    }
97   </programlisting>
98   <para>The <type>std::istream</type> and <type>std::ostream</type> classes
99      are the abstract parents of
100      the various concrete implementations.  If you are only using the
101      interfaces, then you only need to use the appropriate interface header.
102   </para>
103   <para><emphasis><filename class="headerfile">&lt;iomanip&gt;</filename></emphasis>
104      provides "extractors and inserters that alter information maintained by
105      class <classname>ios_base</classname> and its derived classes,"
106      such as <function>std::setprecision</function> and
107      <function>std::setw</function>.  If you need
108      to write expressions like <code>os &lt;&lt; setw(3);</code> or
109      <code>is &gt;&gt; setbase(8);</code>, you must include
110      <filename class="headerfile">&lt;iomanip&gt;</filename>.
111   </para>
112   <para><emphasis><filename class="headerfile">&lt;sstream&gt;</filename></emphasis>
113      and <emphasis><filename class="headerfile">&lt;fstream&gt;</filename></emphasis>
114      declare the six stringstream and fstream classes.  As they are the
115      standard concrete descendants of <type>istream</type> and <type>ostream</type>,
116      you will already know about them.
117   </para>
118   <para>Finally, <emphasis><filename class="headerfile">&lt;iostream&gt;</filename></emphasis>
119      provides the eight standard global objects
120      (<code>cin</code>, <code>cout</code>, etc).  To do this correctly, this
121      header also provides the contents of the
122      <filename class="headerfile">&lt;istream&gt;</filename> and
123      <filename class="headerfile">&lt;ostream&gt;</filename>
124      headers, but nothing else.  The contents of this header look like:
125   </para>
126   <programlisting>
127    #include &lt;ostream&gt;
128    #include &lt;istream&gt;
129
130    namespace std
131    {
132	extern istream cin;
133	extern ostream cout;
134	....
135
136	// this is explained below
137	<emphasis>static ios_base::Init __foo;</emphasis>    // not its real name
138    }
139   </programlisting>
140   <para>Now, the runtime penalty mentioned previously:  the global objects
141      must be initialized before any of your own code uses them; this is
142      guaranteed by the standard.  Like any other global object, they must
143      be initialized once and only once.  This is typically done with a
144      construct like the one above, and the nested class
145      <classname>ios_base::Init</classname> is
146      specified in the standard for just this reason.
147   </para>
148   <para>How does it work?  Because the header is included before any of your
149      code, the <emphasis>__foo</emphasis> object is constructed before any of
150      your objects.  (Global objects are built in the order in which they
151      are declared, and destroyed in reverse order.)  The first time the
152      constructor runs, the eight stream objects are set up.
153   </para>
154   <para>The <code>static</code> keyword means that each object file compiled
155      from a source file containing
156      <filename class="headerfile">&lt;iostream&gt;</filename> will have its own
157      private copy of <emphasis>__foo</emphasis>.  There is no specified order
158      of construction across object files (it's one of those pesky NP complete
159      problems that make life so interesting), so one copy in each object
160      file means that the stream objects are guaranteed to be set up before
161      any of your code which uses them could run, thereby meeting the
162      requirements of the standard.
163   </para>
164   <para>The penalty, of course, is that after the first copy of
165      <emphasis>__foo</emphasis> is constructed, all the others are just wasted
166      processor time.  The time spent is merely for an increment-and-test
167      inside a function call, but over several dozen or hundreds of object
168      files, that time can add up.  (It's not in a tight loop, either.)
169   </para>
170   <para>The lesson?  Only include
171      <filename class="headerfile">&lt;iostream&gt;</filename> when you need
172      to use one of
173      the standard objects in that source file; you'll pay less startup
174      time.  Only include the header files you need to in general; your
175      compile times will go down when there's less parsing work to do.
176   </para>
177
178</section>
179
180<!-- Sect1 02 : Stream Buffers -->
181<section xml:id="std.io.streambufs" xreflabel="Stream Buffers"><info><title>Stream Buffers</title></info>
182<?dbhtml filename="streambufs.html"?>
183
184
185  <section xml:id="io.streambuf.derived" xreflabel="Derived streambuf Classes"><info><title>Derived streambuf Classes</title></info>
186
187    <para>
188    </para>
189
190   <para>Creating your own stream buffers for I/O can be remarkably easy.
191      If you are interested in doing so, we highly recommend two very
192      excellent books:
193      <link xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="http://www.angelikalanger.com/iostreams.html">Standard C++
194      IOStreams and Locales</link> by Langer and Kreft, ISBN 0-201-18395-1, and
195      <link xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="http://www.josuttis.com/libbook/">The C++ Standard Library</link>
196      by Nicolai Josuttis, ISBN 0-201-37926-0.  Both are published by
197      Addison-Wesley, who isn't paying us a cent for saying that, honest.
198   </para>
199   <para>Here is a simple example, io/outbuf1, from the Josuttis text.  It
200      transforms everything sent through it to uppercase.  This version
201      assumes many things about the nature of the character type being
202      used (for more information, read the books or the newsgroups):
203   </para>
204   <programlisting>
205    #include &lt;iostream&gt;
206    #include &lt;streambuf&gt;
207    #include &lt;locale&gt;
208    #include &lt;cstdio&gt;
209
210    class outbuf : public std::streambuf
211    {
212      protected:
213	/* central output function
214	 * - print characters in uppercase mode
215	 */
216	virtual int_type overflow (int_type c) {
217	    if (c != EOF) {
218		// convert lowercase to uppercase
219		c = std::toupper(static_cast&lt;char&gt;(c),getloc());
220
221		// and write the character to the standard output
222		if (putchar(c) == EOF) {
223		    return EOF;
224		}
225	    }
226	    return c;
227	}
228    };
229
230    int main()
231    {
232	// create special output buffer
233	outbuf ob;
234	// initialize output stream with that output buffer
235	std::ostream out(&amp;ob);
236
237	out &lt;&lt; "31 hexadecimal: "
238	    &lt;&lt; std::hex &lt;&lt; 31 &lt;&lt; std::endl;
239	return 0;
240    }
241   </programlisting>
242   <para>Try it yourself!  More examples can be found in 3.1.x code, in
243      <filename>include/ext/*_filebuf.h</filename>, and in this article by James Kanze:
244      <link xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="http://kanze.james.neuf.fr/articles/fltrsbf1.html">Filtering
245      Streambufs</link>.
246   </para>
247
248  </section>
249
250  <section xml:id="io.streambuf.buffering" xreflabel="Buffering"><info><title>Buffering</title></info>
251
252   <para>First, are you sure that you understand buffering?  Particularly
253      the fact that C++ may not, in fact, have anything to do with it?
254   </para>
255   <para>The rules for buffering can be a little odd, but they aren't any
256      different from those of C.  (Maybe that's why they can be a bit
257      odd.)  Many people think that writing a newline to an output
258      stream automatically flushes the output buffer.  This is true only
259      when the output stream is, in fact, a terminal and not a file
260      or some other device -- and <emphasis>that</emphasis> may not even be true
261      since C++ says nothing about files nor terminals.  All of that is
262      system-dependent.  (The "newline-buffer-flushing only occurring
263      on terminals" thing is mostly true on Unix systems, though.)
264   </para>
265   <para>Some people also believe that sending <code>endl</code> down an
266      output stream only writes a newline.  This is incorrect; after a
267      newline is written, the buffer is also flushed.  Perhaps this
268      is the effect you want when writing to a screen -- get the text
269      out as soon as possible, etc -- but the buffering is largely
270      wasted when doing this to a file:
271   </para>
272   <programlisting>
273   output &lt;&lt; "a line of text" &lt;&lt; endl;
274   output &lt;&lt; some_data_variable &lt;&lt; endl;
275   output &lt;&lt; "another line of text" &lt;&lt; endl; </programlisting>
276   <para>The proper thing to do in this case to just write the data out
277      and let the libraries and the system worry about the buffering.
278      If you need a newline, just write a newline:
279   </para>
280   <programlisting>
281   output &lt;&lt; "a line of text\n"
282	  &lt;&lt; some_data_variable &lt;&lt; '\n'
283	  &lt;&lt; "another line of text\n"; </programlisting>
284   <para>I have also joined the output statements into a single statement.
285      You could make the code prettier by moving the single newline to
286      the start of the quoted text on the last line, for example.
287   </para>
288   <para>If you do need to flush the buffer above, you can send an
289      <code>endl</code> if you also need a newline, or just flush the buffer
290      yourself:
291   </para>
292   <programlisting>
293   output &lt;&lt; ...... &lt;&lt; flush;    // can use std::flush manipulator
294   output.flush();               // or call a member fn </programlisting>
295   <para>On the other hand, there are times when writing to a file should
296      be like writing to standard error; no buffering should be done
297      because the data needs to appear quickly (a prime example is a
298      log file for security-related information).  The way to do this is
299      just to turn off the buffering <emphasis>before any I/O operations at
300      all</emphasis> have been done (note that opening counts as an I/O operation):
301   </para>
302   <programlisting>
303   std::ofstream    os;
304   std::ifstream    is;
305   int   i;
306
307   os.rdbuf()-&gt;pubsetbuf(0,0);
308   is.rdbuf()-&gt;pubsetbuf(0,0);
309
310   os.open("/foo/bar/baz");
311   is.open("/qux/quux/quuux");
312   ...
313   os &lt;&lt; "this data is written immediately\n";
314   is &gt;&gt; i;   // and this will probably cause a disk read </programlisting>
315   <para>Since all aspects of buffering are handled by a streambuf-derived
316      member, it is necessary to get at that member with <code>rdbuf()</code>.
317      Then the public version of <code>setbuf</code> can be called.  The
318      arguments are the same as those for the Standard C I/O Library
319      function (a buffer area followed by its size).
320   </para>
321   <para>A great deal of this is implementation-dependent.  For example,
322      <code>streambuf</code> does not specify any actions for its own
323      <code>setbuf()</code>-ish functions; the classes derived from
324      <code>streambuf</code> each define behavior that "makes
325      sense" for that class:  an argument of (0,0) turns off buffering
326      for <code>filebuf</code> but does nothing at all for its siblings
327      <code>stringbuf</code> and <code>strstreambuf</code>, and specifying
328      anything other than (0,0) has varying effects.
329      User-defined classes derived from <code>streambuf</code> can
330      do whatever they want.  (For <code>filebuf</code> and arguments for
331      <code>(p,s)</code> other than zeros, libstdc++ does what you'd expect:
332      the first <code>s</code> bytes of <code>p</code> are used as a buffer,
333      which you must allocate and deallocate.)
334   </para>
335   <para>A last reminder:  there are usually more buffers involved than
336      just those at the language/library level.  Kernel buffers, disk
337      buffers, and the like will also have an effect.  Inspecting and
338      changing those are system-dependent.
339   </para>
340
341  </section>
342</section>
343
344<!-- Sect1 03 : Memory-based Streams -->
345<section xml:id="std.io.memstreams" xreflabel="Memory Streams"><info><title>Memory Based Streams</title></info>
346<?dbhtml filename="stringstreams.html"?>
347
348  <section xml:id="std.io.memstreams.compat" xreflabel="Compatibility strstream"><info><title>Compatibility With strstream</title></info>
349
350    <para>
351    </para>
352   <para>Stringstreams (defined in the header <code>&lt;sstream&gt;</code>)
353      are in this author's opinion one of the coolest things since
354      sliced time.  An example of their use is in the Received Wisdom
355      section for Sect1 21 (Strings),
356      <link linkend="strings.string.Cstring"> describing how to
357      format strings</link>.
358   </para>
359   <para>The quick definition is:  they are siblings of ifstream and ofstream,
360      and they do for <code>std::string</code> what their siblings do for
361      files.  All that work you put into writing <code>&lt;&lt;</code> and
362      <code>&gt;&gt;</code> functions for your classes now pays off
363      <emphasis>again!</emphasis>  Need to format a string before passing the string
364      to a function?  Send your stuff via <code>&lt;&lt;</code> to an
365      ostringstream.  You've read a string as input and need to parse it?
366      Initialize an istringstream with that string, and then pull pieces
367      out of it with <code>&gt;&gt;</code>.  Have a stringstream and need to
368      get a copy of the string inside?  Just call the <code>str()</code>
369      member function.
370   </para>
371   <para>This only works if you've written your
372      <code>&lt;&lt;</code>/<code>&gt;&gt;</code> functions correctly, though,
373      and correctly means that they take istreams and ostreams as
374      parameters, not i<emphasis>f</emphasis>streams and o<emphasis>f</emphasis>streams.  If they
375      take the latter, then your I/O operators will work fine with
376      file streams, but with nothing else -- including stringstreams.
377   </para>
378   <para>If you are a user of the strstream classes, you need to update
379      your code.  You don't have to explicitly append <code>ends</code> to
380      terminate the C-style character array, you don't have to mess with
381      "freezing" functions, and you don't have to manage the
382      memory yourself.  The strstreams have been officially deprecated,
383      which means that 1) future revisions of the C++ Standard won't
384      support them, and 2) if you use them, people will laugh at you.
385   </para>
386
387
388  </section>
389</section>
390
391<!-- Sect1 04 : File-based Streams -->
392<section xml:id="std.io.filestreams" xreflabel="File Streams"><info><title>File Based Streams</title></info>
393<?dbhtml filename="fstreams.html"?>
394
395
396  <section xml:id="std.io.filestreams.copying_a_file" xreflabel="Copying a File"><info><title>Copying a File</title></info>
397
398  <para>
399  </para>
400
401   <para>So you want to copy a file quickly and easily, and most important,
402      completely portably.  And since this is C++, you have an open
403      ifstream (call it IN) and an open ofstream (call it OUT):
404   </para>
405   <programlisting>
406   #include &lt;fstream&gt;
407
408   std::ifstream  IN ("input_file");
409   std::ofstream  OUT ("output_file"); </programlisting>
410   <para>Here's the easiest way to get it completely wrong:
411   </para>
412   <programlisting>
413   OUT &lt;&lt; IN;</programlisting>
414   <para>For those of you who don't already know why this doesn't work
415      (probably from having done it before), I invite you to quickly
416      create a simple text file called "input_file" containing
417      the sentence
418   </para>
419      <programlisting>
420      The quick brown fox jumped over the lazy dog.</programlisting>
421   <para>surrounded by blank lines.  Code it up and try it.  The contents
422      of "output_file" may surprise you.
423   </para>
424   <para>Seriously, go do it.  Get surprised, then come back.  It's worth it.
425   </para>
426   <para>The thing to remember is that the <code>basic_[io]stream</code> classes
427      handle formatting, nothing else.  In particular, they break up on
428      whitespace.  The actual reading, writing, and storing of data is
429      handled by the <code>basic_streambuf</code> family.  Fortunately, the
430      <code>operator&lt;&lt;</code> is overloaded to take an ostream and
431      a pointer-to-streambuf, in order to help with just this kind of
432      "dump the data verbatim" situation.
433   </para>
434   <para>Why a <emphasis>pointer</emphasis> to streambuf and not just a streambuf?  Well,
435      the [io]streams hold pointers (or references, depending on the
436      implementation) to their buffers, not the actual
437      buffers.  This allows polymorphic behavior on the chapter of the buffers
438      as well as the streams themselves.  The pointer is easily retrieved
439      using the <code>rdbuf()</code> member function.  Therefore, the easiest
440      way to copy the file is:
441   </para>
442   <programlisting>
443   OUT &lt;&lt; IN.rdbuf();</programlisting>
444   <para>So what <emphasis>was</emphasis> happening with OUT&lt;&lt;IN?  Undefined
445      behavior, since that particular &lt;&lt; isn't defined by the Standard.
446      I have seen instances where it is implemented, but the character
447      extraction process removes all the whitespace, leaving you with no
448      blank lines and only "Thequickbrownfox...".  With
449      libraries that do not define that operator, IN (or one of IN's
450      member pointers) sometimes gets converted to a void*, and the output
451      file then contains a perfect text representation of a hexadecimal
452      address (quite a big surprise).  Others don't compile at all.
453   </para>
454   <para>Also note that none of this is specific to o<emphasis>*f*</emphasis>streams.
455      The operators shown above are all defined in the parent
456      basic_ostream class and are therefore available with all possible
457      descendants.
458   </para>
459
460  </section>
461
462  <section xml:id="std.io.filestreams.binary" xreflabel="Binary Input and Output"><info><title>Binary Input and Output</title></info>
463
464    <para>
465    </para>
466   <para>The first and most important thing to remember about binary I/O is
467      that opening a file with <code>ios::binary</code> is not, repeat
468      <emphasis>not</emphasis>, the only thing you have to do.  It is not a silver
469      bullet, and will not allow you to use the <code>&lt;&lt;/&gt;&gt;</code>
470      operators of the normal fstreams to do binary I/O.
471   </para>
472   <para>Sorry.  Them's the breaks.
473   </para>
474   <para>This isn't going to try and be a complete tutorial on reading and
475      writing binary files (because "binary"
476      covers a lot of ground), but we will try and clear
477      up a couple of misconceptions and common errors.
478   </para>
479   <para>First, <code>ios::binary</code> has exactly one defined effect, no more
480      and no less.  Normal text mode has to be concerned with the newline
481      characters, and the runtime system will translate between (for
482      example) '\n' and the appropriate end-of-line sequence (LF on Unix,
483      CRLF on DOS, CR on Macintosh, etc).  (There are other things that
484      normal mode does, but that's the most obvious.)  Opening a file in
485      binary mode disables this conversion, so reading a CRLF sequence
486      under Windows won't accidentally get mapped to a '\n' character, etc.
487      Binary mode is not supposed to suddenly give you a bitstream, and
488      if it is doing so in your program then you've discovered a bug in
489      your vendor's compiler (or some other chapter of the C++ implementation,
490      possibly the runtime system).
491   </para>
492   <para>Second, using <code>&lt;&lt;</code> to write and <code>&gt;&gt;</code> to
493      read isn't going to work with the standard file stream classes, even
494      if you use <code>skipws</code> during reading.  Why not?  Because
495      ifstream and ofstream exist for the purpose of <emphasis>formatting</emphasis>,
496      not reading and writing.  Their job is to interpret the data into
497      text characters, and that's exactly what you don't want to happen
498      during binary I/O.
499   </para>
500   <para>Third, using the <code>get()</code> and <code>put()/write()</code> member
501      functions still aren't guaranteed to help you.  These are
502      "unformatted" I/O functions, but still character-based.
503      (This may or may not be what you want, see below.)
504   </para>
505   <para>Notice how all the problems here are due to the inappropriate use
506      of <emphasis>formatting</emphasis> functions and classes to perform something
507      which <emphasis>requires</emphasis> that formatting not be done?  There are a
508      seemingly infinite number of solutions, and a few are listed here:
509   </para>
510   <itemizedlist>
511      <listitem>
512	<para><quote>Derive your own fstream-type classes and write your own
513	  &lt;&lt;/&gt;&gt; operators to do binary I/O on whatever data
514	  types you're using.</quote>
515	</para>
516	<para>
517	  This is a Bad Thing, because while
518	  the compiler would probably be just fine with it, other humans
519	  are going to be confused.  The overloaded bitshift operators
520	  have a well-defined meaning (formatting), and this breaks it.
521	</para>
522      </listitem>
523      <listitem>
524	<para>
525	  <quote>Build the file structure in memory, then
526	  <code>mmap()</code> the file and copy the
527	  structure.
528	</quote>
529	</para>
530	<para>
531	  Well, this is easy to make work, and easy to break, and is
532	  pretty equivalent to using <code>::read()</code> and
533	  <code>::write()</code> directly, and makes no use of the
534	  iostream library at all...
535	  </para>
536      </listitem>
537      <listitem>
538	<para>
539	  <quote>Use streambufs, that's what they're there for.</quote>
540	</para>
541	<para>
542	  While not trivial for the beginner, this is the best of all
543	  solutions.  The streambuf/filebuf layer is the layer that is
544	  responsible for actual I/O.  If you want to use the C++
545	  library for binary I/O, this is where you start.
546	</para>
547      </listitem>
548   </itemizedlist>
549   <para>How to go about using streambufs is a bit beyond the scope of this
550      document (at least for now), but while streambufs go a long way,
551      they still leave a couple of things up to you, the programmer.
552      As an example, byte ordering is completely between you and the
553      operating system, and you have to handle it yourself.
554   </para>
555   <para>Deriving a streambuf or filebuf
556      class from the standard ones, one that is specific to your data
557      types (or an abstraction thereof) is probably a good idea, and
558      lots of examples exist in journals and on Usenet.  Using the
559      standard filebufs directly (either by declaring your own or by
560      using the pointer returned from an fstream's <code>rdbuf()</code>)
561      is certainly feasible as well.
562   </para>
563   <para>One area that causes problems is trying to do bit-by-bit operations
564      with filebufs.  C++ is no different from C in this respect:  I/O
565      must be done at the byte level.  If you're trying to read or write
566      a few bits at a time, you're going about it the wrong way.  You
567      must read/write an integral number of bytes and then process the
568      bytes.  (For example, the streambuf functions take and return
569      variables of type <code>int_type</code>.)
570   </para>
571   <para>Another area of problems is opening text files in binary mode.
572      Generally, binary mode is intended for binary files, and opening
573      text files in binary mode means that you now have to deal with all of
574      those end-of-line and end-of-file problems that we mentioned before.
575   </para>
576   <para>
577      An instructive thread from comp.lang.c++.moderated delved off into
578      this topic starting more or less at
579      <link xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="http://groups.google.com/group/comp.std.c++/browse_thread/thread/f87b4abd7954a87/946a3eb9921e382d?q=comp.std.c%2B%2B+binary+iostream#946a3eb9921e382d">this</link>
580      post and continuing to the end of the thread. (The subject heading is "binary iostreams" on both comp.std.c++
581      and comp.lang.c++.moderated.) Take special note of the replies by James Kanze and Dietmar Kühl.
582   </para>
583    <para>Briefly, the problems of byte ordering and type sizes mean that
584      the unformatted functions like <code>ostream::put()</code> and
585      <code>istream::get()</code> cannot safely be used to communicate
586      between arbitrary programs, or across a network, or from one
587      invocation of a program to another invocation of the same program
588      on a different platform, etc.
589   </para>
590 </section>
591
592</section>
593
594<!-- Sect1 03 : Interacting with C -->
595<section xml:id="std.io.c" xreflabel="Interacting with C"><info><title>Interacting with C</title></info>
596<?dbhtml filename="io_and_c.html"?>
597
598
599
600  <section xml:id="std.io.c.FILE" xreflabel="Using FILE* and file descriptors"><info><title>Using FILE* and file descriptors</title></info>
601
602    <para>
603      See the <link linkend="manual.ext.io">extensions</link> for using
604      <type>FILE</type> and <type>file descriptors</type> with
605      <classname>ofstream</classname> and
606      <classname>ifstream</classname>.
607    </para>
608  </section>
609
610  <section xml:id="std.io.c.sync" xreflabel="Performance Issues"><info><title>Performance</title></info>
611
612    <para>
613      Pathetic Performance? Ditch C.
614    </para>
615   <para>It sounds like a flame on C, but it isn't.  Really.  Calm down.
616      I'm just saying it to get your attention.
617   </para>
618   <para>Because the C++ library includes the C library, both C-style and
619      C++-style I/O have to work at the same time.  For example:
620   </para>
621   <programlisting>
622     #include &lt;iostream&gt;
623     #include &lt;cstdio&gt;
624
625     std::cout &lt;&lt; "Hel";
626     std::printf ("lo, worl");
627     std::cout &lt;&lt; "d!\n";
628   </programlisting>
629   <para>This must do what you think it does.
630   </para>
631   <para>Alert members of the audience will immediately notice that buffering
632      is going to make a hash of the output unless special steps are taken.
633   </para>
634   <para>The special steps taken by libstdc++, at least for version 3.0,
635      involve doing very little buffering for the standard streams, leaving
636      most of the buffering to the underlying C library.  (This kind of
637      thing is tricky to get right.)
638      The upside is that correctness is ensured.  The downside is that
639      writing through <code>cout</code> can quite easily lead to awful
640      performance when the C++ I/O library is layered on top of the C I/O
641      library (as it is for 3.0 by default).  Some patches have been applied
642      which improve the situation for 3.1.
643   </para>
644   <para>However, the C and C++ standard streams only need to be kept in sync
645      when both libraries' facilities are in use.  If your program only uses
646      C++ I/O, then there's no need to sync with the C streams.  The right
647      thing to do in this case is to call
648   </para>
649   <programlisting>
650     #include <emphasis>any of the I/O headers such as ios, iostream, etc</emphasis>
651
652     std::ios::sync_with_stdio(false);
653   </programlisting>
654   <para>You must do this before performing any I/O via the C++ stream objects.
655      Once you call this, the C++ streams will operate independently of the
656      (unused) C streams.  For GCC 3.x, this means that <code>cout</code> and
657      company will become fully buffered on their own.
658   </para>
659   <para>Note, by the way, that the synchronization requirement only applies to
660      the standard streams (<code>cin</code>, <code>cout</code>,
661      <code>cerr</code>,
662      <code>clog</code>, and their wide-character counterparts).  File stream
663      objects that you declare yourself have no such requirement and are fully
664      buffered.
665   </para>
666
667
668  </section>
669</section>
670
671</chapter>
672