]> rtime.felk.cvut.cz Git - l4.git/blob - l4/pkg/libstdc++-v3/contrib/libstdc++-v3-4.4/doc/html/manual/fstreams.html
update
[l4.git] / l4 / pkg / libstdc++-v3 / contrib / libstdc++-v3-4.4 / doc / html / manual / fstreams.html
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">
3 <html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><title>Chapter 27. File Based Streams</title><meta name="generator" content="DocBook XSL Stylesheets V1.74.0" /><meta name="keywords" content="&#10;      ISO C++&#10;    , &#10;      library&#10;    " /><link rel="home" href="../spine.html" title="The GNU C++ Library Documentation" /><link rel="up" href="io.html" title="Part XI.  Input and Output" /><link rel="prev" href="stringstreams.html" title="Chapter 26. Memory Based Streams" /><link rel="next" href="bk01pt11ch27s02.html" title="Binary Input and Output" /></head><body><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="3" align="center">Chapter 27. File Based Streams</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="stringstreams.html">Prev</a> </td><th width="60%" align="center">Part XI. 
4   Input and Output
5   
6 </th><td width="20%" align="right"> <a accesskey="n" href="bk01pt11ch27s02.html">Next</a></td></tr></table><hr /></div><div class="chapter" lang="en" xml:lang="en"><div class="titlepage"><div><div><h2 class="title"><a id="manual.io.filestreams"></a>Chapter 27. File Based Streams</h2></div></div></div><div class="toc"><p><b>Table of Contents</b></p><dl><dt><span class="sect1"><a href="fstreams.html#manual.io.filestreams.copying_a_file">Copying a File</a></span></dt><dt><span class="sect1"><a href="bk01pt11ch27s02.html">Binary Input and Output</a></span></dt><dt><span class="sect1"><a href="bk01pt11ch27s03.html">More Binary Input and Output</a></span></dt></dl></div><div class="sect1" lang="en" xml:lang="en"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a id="manual.io.filestreams.copying_a_file"></a>Copying a File</h2></div></div></div><p>
7   </p><p>So you want to copy a file quickly and easily, and most important,
8       completely portably.  And since this is C++, you have an open
9       ifstream (call it IN) and an open ofstream (call it OUT):
10    </p><pre class="programlisting">
11    #include &lt;fstream&gt;
12
13    std::ifstream  IN ("input_file");
14    std::ofstream  OUT ("output_file"); </pre><p>Here's the easiest way to get it completely wrong:
15    </p><pre class="programlisting">
16    OUT &lt;&lt; IN;</pre><p>For those of you who don't already know why this doesn't work
17       (probably from having done it before), I invite you to quickly
18       create a simple text file called "input_file" containing
19       the sentence
20    </p><pre class="programlisting">
21       The quick brown fox jumped over the lazy dog.</pre><p>surrounded by blank lines.  Code it up and try it.  The contents
22       of "output_file" may surprise you.
23    </p><p>Seriously, go do it.  Get surprised, then come back.  It's worth it.
24    </p><p>The thing to remember is that the <code class="code">basic_[io]stream</code> classes
25       handle formatting, nothing else.  In particular, they break up on
26       whitespace.  The actual reading, writing, and storing of data is
27       handled by the <code class="code">basic_streambuf</code> family.  Fortunately, the
28       <code class="code">operator&lt;&lt;</code> is overloaded to take an ostream and
29       a pointer-to-streambuf, in order to help with just this kind of
30       "dump the data verbatim" situation.
31    </p><p>Why a <span class="emphasis"><em>pointer</em></span> to streambuf and not just a streambuf?  Well,
32       the [io]streams hold pointers (or references, depending on the
33       implementation) to their buffers, not the actual
34       buffers.  This allows polymorphic behavior on the part of the buffers
35       as well as the streams themselves.  The pointer is easily retrieved
36       using the <code class="code">rdbuf()</code> member function.  Therefore, the easiest
37       way to copy the file is:
38    </p><pre class="programlisting">
39    OUT &lt;&lt; IN.rdbuf();</pre><p>So what <span class="emphasis"><em>was</em></span> happening with OUT&lt;&lt;IN?  Undefined
40       behavior, since that particular &lt;&lt; isn't defined by the Standard.
41       I have seen instances where it is implemented, but the character
42       extraction process removes all the whitespace, leaving you with no
43       blank lines and only "Thequickbrownfox...".  With
44       libraries that do not define that operator, IN (or one of IN's
45       member pointers) sometimes gets converted to a void*, and the output
46       file then contains a perfect text representation of a hexadecimal
47       address (quite a big surprise).  Others don't compile at all.
48    </p><p>Also note that none of this is specific to o<span class="emphasis"><em>*f*</em></span>streams. 
49       The operators shown above are all defined in the parent 
50       basic_ostream class and are therefore available with all possible
51       descendants.
52    </p></div></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="stringstreams.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="bk01pt11ch27s02.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">Chapter 26. Memory Based Streams </td><td width="20%" align="center"><a accesskey="h" href="../spine.html">Home</a></td><td width="40%" align="right" valign="top"> Binary Input and Output</td></tr></table></div></body></html>