1 |
424 |
jeremybenn |
<?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>Binary Input and Output</title><meta name="generator" content="DocBook XSL Stylesheets V1.75.2" /><meta name="keywords" content=" ISO C++ , library " /><link rel="home" href="../spine.html" title="The GNU C++ Library Documentation" /><link rel="up" href="fstreams.html" title="Chapter 27. File Based Streams" /><link rel="prev" href="fstreams.html" title="Chapter 27. File Based Streams" /><link rel="next" href="io_and_c.html" title="Chapter 28. Interacting with C" /></head><body><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="3" align="center">Binary Input and Output</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="fstreams.html">Prev</a> </td><th width="60%" align="center">Chapter 27. File Based Streams</th><td width="20%" align="right"> <a accesskey="n" href="io_and_c.html">Next</a></td></tr></table><hr /></div><div class="sect1" title="Binary Input and Output"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a id="manual.io.filestreams.binary"></a>Binary Input and Output</h2></div></div></div><p>
|
4 |
|
|
</p><p>The first and most important thing to remember about binary I/O is
|
5 |
|
|
that opening a file with <code class="code">ios::binary</code> is not, repeat
|
6 |
|
|
<span class="emphasis"><em>not</em></span>, the only thing you have to do. It is not a silver
|
7 |
|
|
bullet, and will not allow you to use the <code class="code"><</>></code>
|
8 |
|
|
operators of the normal fstreams to do binary I/O.
|
9 |
|
|
</p><p>Sorry. Them's the breaks.
|
10 |
|
|
</p><p>This isn't going to try and be a complete tutorial on reading and
|
11 |
|
|
writing binary files (because "binary"
|
12 |
|
|
covers a lot of ground), but we will try and clear
|
13 |
|
|
up a couple of misconceptions and common errors.
|
14 |
|
|
</p><p>First, <code class="code">ios::binary</code> has exactly one defined effect, no more
|
15 |
|
|
and no less. Normal text mode has to be concerned with the newline
|
16 |
|
|
characters, and the runtime system will translate between (for
|
17 |
|
|
example) '\n' and the appropriate end-of-line sequence (LF on Unix,
|
18 |
|
|
CRLF on DOS, CR on Macintosh, etc). (There are other things that
|
19 |
|
|
normal mode does, but that's the most obvious.) Opening a file in
|
20 |
|
|
binary mode disables this conversion, so reading a CRLF sequence
|
21 |
|
|
under Windows won't accidentally get mapped to a '\n' character, etc.
|
22 |
|
|
Binary mode is not supposed to suddenly give you a bitstream, and
|
23 |
|
|
if it is doing so in your program then you've discovered a bug in
|
24 |
|
|
your vendor's compiler (or some other part of the C++ implementation,
|
25 |
|
|
possibly the runtime system).
|
26 |
|
|
</p><p>Second, using <code class="code"><<</code> to write and <code class="code">>></code> to
|
27 |
|
|
read isn't going to work with the standard file stream classes, even
|
28 |
|
|
if you use <code class="code">skipws</code> during reading. Why not? Because
|
29 |
|
|
ifstream and ofstream exist for the purpose of <span class="emphasis"><em>formatting</em></span>,
|
30 |
|
|
not reading and writing. Their job is to interpret the data into
|
31 |
|
|
text characters, and that's exactly what you don't want to happen
|
32 |
|
|
during binary I/O.
|
33 |
|
|
</p><p>Third, using the <code class="code">get()</code> and <code class="code">put()/write()</code> member
|
34 |
|
|
functions still aren't guaranteed to help you. These are
|
35 |
|
|
"unformatted" I/O functions, but still character-based.
|
36 |
|
|
(This may or may not be what you want, see below.)
|
37 |
|
|
</p><p>Notice how all the problems here are due to the inappropriate use
|
38 |
|
|
of <span class="emphasis"><em>formatting</em></span> functions and classes to perform something
|
39 |
|
|
which <span class="emphasis"><em>requires</em></span> that formatting not be done? There are a
|
40 |
|
|
seemingly infinite number of solutions, and a few are listed here:
|
41 |
|
|
</p><div class="itemizedlist"><ul class="itemizedlist" type="disc"><li class="listitem"><p><span class="quote">“<span class="quote">Derive your own fstream-type classes and write your own
|
42 |
|
|
<</>> operators to do binary I/O on whatever data
|
43 |
|
|
types you're using.</span>”</span>
|
44 |
|
|
</p><p>
|
45 |
|
|
This is a Bad Thing, because while
|
46 |
|
|
the compiler would probably be just fine with it, other humans
|
47 |
|
|
are going to be confused. The overloaded bitshift operators
|
48 |
|
|
have a well-defined meaning (formatting), and this breaks it.
|
49 |
|
|
</p></li><li class="listitem"><p>
|
50 |
|
|
<span class="quote">“<span class="quote">Build the file structure in memory, then
|
51 |
|
|
<code class="code">mmap()</code> the file and copy the
|
52 |
|
|
structure.
|
53 |
|
|
</span>”</span>
|
54 |
|
|
</p><p>
|
55 |
|
|
Well, this is easy to make work, and easy to break, and is
|
56 |
|
|
pretty equivalent to using <code class="code">::read()</code> and
|
57 |
|
|
<code class="code">::write()</code> directly, and makes no use of the
|
58 |
|
|
iostream library at all...
|
59 |
|
|
</p></li><li class="listitem"><p>
|
60 |
|
|
<span class="quote">“<span class="quote">Use streambufs, that's what they're there for.</span>”</span>
|
61 |
|
|
</p><p>
|
62 |
|
|
While not trivial for the beginner, this is the best of all
|
63 |
|
|
solutions. The streambuf/filebuf layer is the layer that is
|
64 |
|
|
responsible for actual I/O. If you want to use the C++
|
65 |
|
|
library for binary I/O, this is where you start.
|
66 |
|
|
</p></li></ul></div><p>How to go about using streambufs is a bit beyond the scope of this
|
67 |
|
|
document (at least for now), but while streambufs go a long way,
|
68 |
|
|
they still leave a couple of things up to you, the programmer.
|
69 |
|
|
As an example, byte ordering is completely between you and the
|
70 |
|
|
operating system, and you have to handle it yourself.
|
71 |
|
|
</p><p>Deriving a streambuf or filebuf
|
72 |
|
|
class from the standard ones, one that is specific to your data
|
73 |
|
|
types (or an abstraction thereof) is probably a good idea, and
|
74 |
|
|
lots of examples exist in journals and on Usenet. Using the
|
75 |
|
|
standard filebufs directly (either by declaring your own or by
|
76 |
|
|
using the pointer returned from an fstream's <code class="code">rdbuf()</code>)
|
77 |
|
|
is certainly feasible as well.
|
78 |
|
|
</p><p>One area that causes problems is trying to do bit-by-bit operations
|
79 |
|
|
with filebufs. C++ is no different from C in this respect: I/O
|
80 |
|
|
must be done at the byte level. If you're trying to read or write
|
81 |
|
|
a few bits at a time, you're going about it the wrong way. You
|
82 |
|
|
must read/write an integral number of bytes and then process the
|
83 |
|
|
bytes. (For example, the streambuf functions take and return
|
84 |
|
|
variables of type <code class="code">int_type</code>.)
|
85 |
|
|
</p><p>Another area of problems is opening text files in binary mode.
|
86 |
|
|
Generally, binary mode is intended for binary files, and opening
|
87 |
|
|
text files in binary mode means that you now have to deal with all of
|
88 |
|
|
those end-of-line and end-of-file problems that we mentioned before.
|
89 |
|
|
</p><p>
|
90 |
|
|
An instructive thread from comp.lang.c++.moderated delved off into
|
91 |
|
|
this topic starting more or less at
|
92 |
|
|
<a class="ulink" href="http://groups.google.com/group/comp.std.c++/browse_thread/thread/f87b4abd7954a87/946a3eb9921e382d?q=comp.std.c%2B%2B+binary+iostream#946a3eb9921e382d" target="_top">this</a>
|
93 |
|
|
post and continuing to the end of the thread. (The subject heading is "binary iostreams" on both comp.std.c++
|
94 |
|
|
and comp.lang.c++.moderated.) Take special note of the replies by James Kanze and Dietmar Kühl.
|
95 |
|
|
</p><p>Briefly, the problems of byte ordering and type sizes mean that
|
96 |
|
|
the unformatted functions like <code class="code">ostream::put()</code> and
|
97 |
|
|
<code class="code">istream::get()</code> cannot safely be used to communicate
|
98 |
|
|
between arbitrary programs, or across a network, or from one
|
99 |
|
|
invocation of a program to another invocation of the same program
|
100 |
|
|
on a different platform, etc.
|
101 |
|
|
</p></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="fstreams.html">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="fstreams.html">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="io_and_c.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">Chapter 27. File Based Streams </td><td width="20%" align="center"><a accesskey="h" href="../spine.html">Home</a></td><td width="40%" align="right" valign="top"> Chapter 28. Interacting with C</td></tr></table></div></body></html>
|