[LC++]Copying binary files with operator<< and rdbuf()

Carlo Wood carlo at alinoe.com
Sat Nov 22 08:08:03 UTC 2003


I think that ios_base::binary turns off Locale translations etc.
But that doesn't change any formatting that is done.  Formatting
is done by operator<< functions, so you don't want to use those
for binary I/O.  If you use the streambuf interface, or the
read()/write() methods, directly then you are fine.

I see << as a serializer: it translates a (complex) object into
a stream.  It can do this in 101 ways - and usually even dynamically
dependend on certain (formatting) flags.  Most << functions are
implemented by means of calling other << functions (as well as
adding extra strings, determining the order of serializing/printing
and determining the formatting of the sub elements) down to the
standard << serializers for builtin types... which are implemented
to output printable characters that mean something to humans when
interpretted as ASCII (and that I don't call binary), lets call
that 'text' as (opposed to 'binary').  Result: most << functions
write out 'text'.

If you want to do binary, don't use the << operators.

-- 
Carlo Wood <carlo at alinoe.com>



More information about the tuxCPProgramming mailing list