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.73.2" /><meta name="keywords" content=" ISO C++ , library " /><link rel="start" 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="bk01pt11ch26.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="bk01pt11ch26.html">Prev</a> </td><th width="60%" align="center">Part XI. Input and Output</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="bk01pt11ch27.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>
4 </p><p>So you want to copy a file quickly and easily, and most important,
5 completely portably. And since this is C++, you have an open
6 ifstream (call it IN) and an open ofstream (call it OUT):
7 </p><pre class="programlisting">
8 #include <fstream>
10 std::ifstream IN ("input_file");
11 std::ofstream OUT ("output_file"); </pre><p>Here's the easiest way to get it completely wrong:
12 </p><pre class="programlisting">
13 OUT << IN;</pre><p>For those of you who don't already know why this doesn't work
14 (probably from having done it before), I invite you to quickly
15 create a simple text file called "input_file" containing
17 </p><pre class="programlisting">
18 The quick brown fox jumped over the lazy dog.</pre><p>surrounded by blank lines. Code it up and try it. The contents
19 of "output_file" may surprise you.
20 </p><p>Seriously, go do it. Get surprised, then come back. It's worth it.
21 </p><p>The thing to remember is that the <code class="code">basic_[io]stream</code> classes
22 handle formatting, nothing else. In particular, they break up on
23 whitespace. The actual reading, writing, and storing of data is
24 handled by the <code class="code">basic_streambuf</code> family. Fortunately, the
25 <code class="code">operator<<</code> is overloaded to take an ostream and
26 a pointer-to-streambuf, in order to help with just this kind of
27 "dump the data verbatim" situation.
28 </p><p>Why a <span class="emphasis"><em>pointer</em></span> to streambuf and not just a streambuf? Well,
29 the [io]streams hold pointers (or references, depending on the
30 implementation) to their buffers, not the actual
31 buffers. This allows polymorphic behavior on the part of the buffers
32 as well as the streams themselves. The pointer is easily retrieved
33 using the <code class="code">rdbuf()</code> member function. Therefore, the easiest
34 way to copy the file is:
35 </p><pre class="programlisting">
36 OUT << IN.rdbuf();</pre><p>So what <span class="emphasis"><em>was</em></span> happening with OUT<<IN? Undefined
37 behavior, since that particular << isn't defined by the Standard.
38 I have seen instances where it is implemented, but the character
39 extraction process removes all the whitespace, leaving you with no
40 blank lines and only "Thequickbrownfox...". With
41 libraries that do not define that operator, IN (or one of IN's
42 member pointers) sometimes gets converted to a void*, and the output
43 file then contains a perfect text representation of a hexadecimal
44 address (quite a big surprise). Others don't compile at all.
45 </p><p>Also note that none of this is specific to o<span class="emphasis"><em>*f*</em></span>streams.
46 The operators shown above are all defined in the parent
47 basic_ostream class and are therefore available with all possible
49 </p></div></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="bk01pt11ch26.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>