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>Binary Input and Output</title><meta name="generator" content="DocBook XSL Stylesheets V1.74.0" /><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="bk01pt11ch27s03.html" title="More Binary Input and Output" /></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="bk01pt11ch27s03.html">Next</a></td></tr></table><hr /></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.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 <a class="ulink" href="#7" target="_top">covers a lot of ground)</a>, 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
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 type="disc"><li><p>“<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>”
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.
50 “<span class="quote">Build the file structure in memory, then
51 <code class="code">mmap()</code> the file and copy the
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...
60 “<span class="quote">Use streambufs, that's what they're there for.</span>”
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 An instructive thread from comp.lang.c++.moderated delved off into
90 this topic starting more or less at
91 <a class="ulink" href="http://groups.google.com/groups?oi=djq&selm=an_436187505" target="_top">this</a>
92 article and continuing to the end of the thread. (You'll have to
93 sort through some flames every couple of paragraphs, but the points
95 </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="bk01pt11ch27s03.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"> More Binary Input and Output</td></tr></table></div></body></html>