Table of Contents
So you want to copy a file quickly and easily, and most important, completely portably. And since this is C++, you have an open ifstream (call it IN) and an open ofstream (call it OUT):
#include <fstream> std::ifstream IN ("input_file"); std::ofstream OUT ("output_file");
Here's the easiest way to get it completely wrong:
OUT << IN;
For those of you who don't already know why this doesn't work (probably from having done it before), I invite you to quickly create a simple text file called "input_file" containing the sentence
The quick brown fox jumped over the lazy dog.
surrounded by blank lines. Code it up and try it. The contents of "output_file" may surprise you.
Seriously, go do it. Get surprised, then come back. It's worth it.
The thing to remember is that the basic_[io]stream
classes
handle formatting, nothing else. In particular, they break up on
whitespace. The actual reading, writing, and storing of data is
handled by the basic_streambuf
family. Fortunately, the
operator<<
is overloaded to take an ostream and
a pointer-to-streambuf, in order to help with just this kind of
"dump the data verbatim" situation.
Why a pointer to streambuf and not just a streambuf? Well,
the [io]streams hold pointers (or references, depending on the
implementation) to their buffers, not the actual
buffers. This allows polymorphic behavior on the part of the buffers
as well as the streams themselves. The pointer is easily retrieved
using the rdbuf()
member function. Therefore, the easiest
way to copy the file is:
OUT << IN.rdbuf();
So what was happening with OUT<<IN? Undefined behavior, since that particular << isn't defined by the Standard. I have seen instances where it is implemented, but the character extraction process removes all the whitespace, leaving you with no blank lines and only "Thequickbrownfox...". With libraries that do not define that operator, IN (or one of IN's member pointers) sometimes gets converted to a void*, and the output file then contains a perfect text representation of a hexadecimal address (quite a big surprise). Others don't compile at all.
Also note that none of this is specific to o*f*streams. The operators shown above are all defined in the parent basic_ostream class and are therefore available with all possible descendants.