xref: /netbsd-src/external/gpl3/gcc.old/dist/libstdc++-v3/doc/xml/manual/io.xml (revision 1debfc3d3fad8af6f31804271c18e67f77b4d718)
1*1debfc3dSmrg<chapter xmlns="http://docbook.org/ns/docbook" version="5.0"
2*1debfc3dSmrg	 xml:id="std.io" xreflabel="Input and Output">
3*1debfc3dSmrg<?dbhtml filename="io.html"?>
4*1debfc3dSmrg
5*1debfc3dSmrg<info><title>
6*1debfc3dSmrg  Input and Output
7*1debfc3dSmrg  <indexterm><primary>Input and Output</primary></indexterm>
8*1debfc3dSmrg</title>
9*1debfc3dSmrg  <keywordset>
10*1debfc3dSmrg    <keyword>ISO C++</keyword>
11*1debfc3dSmrg    <keyword>library</keyword>
12*1debfc3dSmrg  </keywordset>
13*1debfc3dSmrg</info>
14*1debfc3dSmrg
15*1debfc3dSmrg
16*1debfc3dSmrg
17*1debfc3dSmrg<!-- Sect1 01 : Iostream Objects -->
18*1debfc3dSmrg<section xml:id="std.io.objects" xreflabel="IO Objects"><info><title>Iostream Objects</title></info>
19*1debfc3dSmrg<?dbhtml filename="iostream_objects.html"?>
20*1debfc3dSmrg
21*1debfc3dSmrg
22*1debfc3dSmrg   <para>To minimize the time you have to wait on the compiler, it's good to
23*1debfc3dSmrg      only include the headers you really need.  Many people simply include
24*1debfc3dSmrg      <filename class="headerfile">&lt;iostream&gt;</filename> when they don't
25*1debfc3dSmrg      need to -- and that can <emphasis>penalize your runtime as well.</emphasis>
26*1debfc3dSmrg      Here are some tips on which header to use
27*1debfc3dSmrg      for which situations, starting with the simplest.
28*1debfc3dSmrg   </para>
29*1debfc3dSmrg   <para><emphasis><filename class="headerfile">&lt;iosfwd&gt;</filename></emphasis>
30*1debfc3dSmrg      should be included whenever you simply need the <emphasis>name</emphasis>
31*1debfc3dSmrg      of an I/O-related class, such as "<classname>ofstream</classname>" or
32*1debfc3dSmrg      "<classname>basic_streambuf</classname>".
33*1debfc3dSmrg      Like the name implies, these are forward declarations.
34*1debfc3dSmrg      (A word to all you fellow old school programmers:
35*1debfc3dSmrg      trying to forward declare classes like "<code>class istream;</code>"
36*1debfc3dSmrg      won't work.
37*1debfc3dSmrg      Look in the <filename class="headerfile">&lt;iosfwd&gt;</filename> header
38*1debfc3dSmrg      if you'd like to know why.)  For example,
39*1debfc3dSmrg   </para>
40*1debfc3dSmrg   <programlisting>
41*1debfc3dSmrg    #include &lt;iosfwd&gt;
42*1debfc3dSmrg
43*1debfc3dSmrg    class MyClass
44*1debfc3dSmrg    {
45*1debfc3dSmrg	....
46*1debfc3dSmrg	std::ifstream&amp;   input_file;
47*1debfc3dSmrg    };
48*1debfc3dSmrg
49*1debfc3dSmrg    extern std::ostream&amp; operator&lt;&lt; (std::ostream&amp;, MyClass&amp;);
50*1debfc3dSmrg   </programlisting>
51*1debfc3dSmrg   <para><emphasis><filename class="headerfile">&lt;ios&gt;</filename></emphasis>
52*1debfc3dSmrg      declares the base classes for the entire I/O stream hierarchy,
53*1debfc3dSmrg      <classname>std::ios_base</classname> and <classname>std::basic_ios&lt;charT&gt;</classname>,
54*1debfc3dSmrg      the counting types <type>std::streamoff</type> and <type>std::streamsize</type>,
55*1debfc3dSmrg      the file positioning type <type>std::fpos</type>,
56*1debfc3dSmrg      and the various manipulators like <function>std::hex</function>,
57*1debfc3dSmrg      <function>std::fixed</function>, <function>std::noshowbase</function>,
58*1debfc3dSmrg      and so forth.
59*1debfc3dSmrg   </para>
60*1debfc3dSmrg   <para>The <classname>ios_base</classname> class is what holds the format
61*1debfc3dSmrg      flags, the state flags, and the functions which change them
62*1debfc3dSmrg      (<function>setf()</function>, <function>width()</function>,
63*1debfc3dSmrg      <function>precision()</function>, etc).
64*1debfc3dSmrg      You can also store extra data and register callback functions
65*1debfc3dSmrg      through <classname>ios_base</classname>, but that has been historically
66*1debfc3dSmrg      underused.  Anything
67*1debfc3dSmrg      which doesn't depend on the type of characters stored is consolidated
68*1debfc3dSmrg      here.
69*1debfc3dSmrg   </para>
70*1debfc3dSmrg   <para>The class template <classname>basic_ios</classname> is the highest
71*1debfc3dSmrg      class template in the
72*1debfc3dSmrg      hierarchy; it is the first one depending on the character type, and
73*1debfc3dSmrg      holds all general state associated with that type:  the pointer to the
74*1debfc3dSmrg      polymorphic stream buffer, the facet information, etc.
75*1debfc3dSmrg   </para>
76*1debfc3dSmrg   <para><emphasis><filename class="headerfile">&lt;streambuf&gt;</filename></emphasis>
77*1debfc3dSmrg      declares the class template <classname>basic_streambuf</classname>, and
78*1debfc3dSmrg      two standard instantiations, <type>streambuf</type> and
79*1debfc3dSmrg      <type>wstreambuf</type>.  If you need to work with the vastly useful and
80*1debfc3dSmrg      capable stream buffer classes, e.g., to create a new form of storage
81*1debfc3dSmrg      transport, this header is the one to include.
82*1debfc3dSmrg   </para>
83*1debfc3dSmrg   <para><emphasis><filename class="headerfile">&lt;istream&gt;</filename></emphasis>
84*1debfc3dSmrg       and <emphasis><filename class="headerfile">&lt;ostream&gt;</filename></emphasis>
85*1debfc3dSmrg       are the headers to include when you are using the overloaded
86*1debfc3dSmrg      <code>&gt;&gt;</code> and <code>&lt;&lt;</code> operators,
87*1debfc3dSmrg      or any of the other abstract stream formatting functions.
88*1debfc3dSmrg      For example,
89*1debfc3dSmrg   </para>
90*1debfc3dSmrg   <programlisting>
91*1debfc3dSmrg    #include &lt;istream&gt;
92*1debfc3dSmrg
93*1debfc3dSmrg    std::ostream&amp; operator&lt;&lt; (std::ostream&amp; os, MyClass&amp; c)
94*1debfc3dSmrg    {
95*1debfc3dSmrg       return os &lt;&lt; c.data1() &lt;&lt; c.data2();
96*1debfc3dSmrg    }
97*1debfc3dSmrg   </programlisting>
98*1debfc3dSmrg   <para>The <type>std::istream</type> and <type>std::ostream</type> classes
99*1debfc3dSmrg      are the abstract parents of
100*1debfc3dSmrg      the various concrete implementations.  If you are only using the
101*1debfc3dSmrg      interfaces, then you only need to use the appropriate interface header.
102*1debfc3dSmrg   </para>
103*1debfc3dSmrg   <para><emphasis><filename class="headerfile">&lt;iomanip&gt;</filename></emphasis>
104*1debfc3dSmrg      provides "extractors and inserters that alter information maintained by
105*1debfc3dSmrg      class <classname>ios_base</classname> and its derived classes,"
106*1debfc3dSmrg      such as <function>std::setprecision</function> and
107*1debfc3dSmrg      <function>std::setw</function>.  If you need
108*1debfc3dSmrg      to write expressions like <code>os &lt;&lt; setw(3);</code> or
109*1debfc3dSmrg      <code>is &gt;&gt; setbase(8);</code>, you must include
110*1debfc3dSmrg      <filename class="headerfile">&lt;iomanip&gt;</filename>.
111*1debfc3dSmrg   </para>
112*1debfc3dSmrg   <para><emphasis><filename class="headerfile">&lt;sstream&gt;</filename></emphasis>
113*1debfc3dSmrg      and <emphasis><filename class="headerfile">&lt;fstream&gt;</filename></emphasis>
114*1debfc3dSmrg      declare the six stringstream and fstream classes.  As they are the
115*1debfc3dSmrg      standard concrete descendants of <type>istream</type> and <type>ostream</type>,
116*1debfc3dSmrg      you will already know about them.
117*1debfc3dSmrg   </para>
118*1debfc3dSmrg   <para>Finally, <emphasis><filename class="headerfile">&lt;iostream&gt;</filename></emphasis>
119*1debfc3dSmrg      provides the eight standard global objects
120*1debfc3dSmrg      (<code>cin</code>, <code>cout</code>, etc).  To do this correctly, this
121*1debfc3dSmrg      header also provides the contents of the
122*1debfc3dSmrg      <filename class="headerfile">&lt;istream&gt;</filename> and
123*1debfc3dSmrg      <filename class="headerfile">&lt;ostream&gt;</filename>
124*1debfc3dSmrg      headers, but nothing else.  The contents of this header look like:
125*1debfc3dSmrg   </para>
126*1debfc3dSmrg   <programlisting>
127*1debfc3dSmrg    #include &lt;ostream&gt;
128*1debfc3dSmrg    #include &lt;istream&gt;
129*1debfc3dSmrg
130*1debfc3dSmrg    namespace std
131*1debfc3dSmrg    {
132*1debfc3dSmrg	extern istream cin;
133*1debfc3dSmrg	extern ostream cout;
134*1debfc3dSmrg	....
135*1debfc3dSmrg
136*1debfc3dSmrg	// this is explained below
137*1debfc3dSmrg	<emphasis>static ios_base::Init __foo;</emphasis>    // not its real name
138*1debfc3dSmrg    }
139*1debfc3dSmrg   </programlisting>
140*1debfc3dSmrg   <para>Now, the runtime penalty mentioned previously:  the global objects
141*1debfc3dSmrg      must be initialized before any of your own code uses them; this is
142*1debfc3dSmrg      guaranteed by the standard.  Like any other global object, they must
143*1debfc3dSmrg      be initialized once and only once.  This is typically done with a
144*1debfc3dSmrg      construct like the one above, and the nested class
145*1debfc3dSmrg      <classname>ios_base::Init</classname> is
146*1debfc3dSmrg      specified in the standard for just this reason.
147*1debfc3dSmrg   </para>
148*1debfc3dSmrg   <para>How does it work?  Because the header is included before any of your
149*1debfc3dSmrg      code, the <emphasis>__foo</emphasis> object is constructed before any of
150*1debfc3dSmrg      your objects.  (Global objects are built in the order in which they
151*1debfc3dSmrg      are declared, and destroyed in reverse order.)  The first time the
152*1debfc3dSmrg      constructor runs, the eight stream objects are set up.
153*1debfc3dSmrg   </para>
154*1debfc3dSmrg   <para>The <code>static</code> keyword means that each object file compiled
155*1debfc3dSmrg      from a source file containing
156*1debfc3dSmrg      <filename class="headerfile">&lt;iostream&gt;</filename> will have its own
157*1debfc3dSmrg      private copy of <emphasis>__foo</emphasis>.  There is no specified order
158*1debfc3dSmrg      of construction across object files (it's one of those pesky NP complete
159*1debfc3dSmrg      problems that make life so interesting), so one copy in each object
160*1debfc3dSmrg      file means that the stream objects are guaranteed to be set up before
161*1debfc3dSmrg      any of your code which uses them could run, thereby meeting the
162*1debfc3dSmrg      requirements of the standard.
163*1debfc3dSmrg   </para>
164*1debfc3dSmrg   <para>The penalty, of course, is that after the first copy of
165*1debfc3dSmrg      <emphasis>__foo</emphasis> is constructed, all the others are just wasted
166*1debfc3dSmrg      processor time.  The time spent is merely for an increment-and-test
167*1debfc3dSmrg      inside a function call, but over several dozen or hundreds of object
168*1debfc3dSmrg      files, that time can add up.  (It's not in a tight loop, either.)
169*1debfc3dSmrg   </para>
170*1debfc3dSmrg   <para>The lesson?  Only include
171*1debfc3dSmrg      <filename class="headerfile">&lt;iostream&gt;</filename> when you need
172*1debfc3dSmrg      to use one of
173*1debfc3dSmrg      the standard objects in that source file; you'll pay less startup
174*1debfc3dSmrg      time.  Only include the header files you need to in general; your
175*1debfc3dSmrg      compile times will go down when there's less parsing work to do.
176*1debfc3dSmrg   </para>
177*1debfc3dSmrg
178*1debfc3dSmrg</section>
179*1debfc3dSmrg
180*1debfc3dSmrg<!-- Sect1 02 : Stream Buffers -->
181*1debfc3dSmrg<section xml:id="std.io.streambufs" xreflabel="Stream Buffers"><info><title>Stream Buffers</title></info>
182*1debfc3dSmrg<?dbhtml filename="streambufs.html"?>
183*1debfc3dSmrg
184*1debfc3dSmrg
185*1debfc3dSmrg  <section xml:id="io.streambuf.derived" xreflabel="Derived streambuf Classes"><info><title>Derived streambuf Classes</title></info>
186*1debfc3dSmrg
187*1debfc3dSmrg    <para>
188*1debfc3dSmrg    </para>
189*1debfc3dSmrg
190*1debfc3dSmrg   <para>Creating your own stream buffers for I/O can be remarkably easy.
191*1debfc3dSmrg      If you are interested in doing so, we highly recommend two very
192*1debfc3dSmrg      excellent books:
193*1debfc3dSmrg      <link xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="http://www.angelikalanger.com/iostreams.html">Standard C++
194*1debfc3dSmrg      IOStreams and Locales</link> by Langer and Kreft, ISBN 0-201-18395-1, and
195*1debfc3dSmrg      <link xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="http://www.josuttis.com/libbook/">The C++ Standard Library</link>
196*1debfc3dSmrg      by Nicolai Josuttis, ISBN 0-201-37926-0.  Both are published by
197*1debfc3dSmrg      Addison-Wesley, who isn't paying us a cent for saying that, honest.
198*1debfc3dSmrg   </para>
199*1debfc3dSmrg   <para>Here is a simple example, io/outbuf1, from the Josuttis text.  It
200*1debfc3dSmrg      transforms everything sent through it to uppercase.  This version
201*1debfc3dSmrg      assumes many things about the nature of the character type being
202*1debfc3dSmrg      used (for more information, read the books or the newsgroups):
203*1debfc3dSmrg   </para>
204*1debfc3dSmrg   <programlisting>
205*1debfc3dSmrg    #include &lt;iostream&gt;
206*1debfc3dSmrg    #include &lt;streambuf&gt;
207*1debfc3dSmrg    #include &lt;locale&gt;
208*1debfc3dSmrg    #include &lt;cstdio&gt;
209*1debfc3dSmrg
210*1debfc3dSmrg    class outbuf : public std::streambuf
211*1debfc3dSmrg    {
212*1debfc3dSmrg      protected:
213*1debfc3dSmrg	/* central output function
214*1debfc3dSmrg	 * - print characters in uppercase mode
215*1debfc3dSmrg	 */
216*1debfc3dSmrg	virtual int_type overflow (int_type c) {
217*1debfc3dSmrg	    if (c != EOF) {
218*1debfc3dSmrg		// convert lowercase to uppercase
219*1debfc3dSmrg		c = std::toupper(static_cast&lt;char&gt;(c),getloc());
220*1debfc3dSmrg
221*1debfc3dSmrg		// and write the character to the standard output
222*1debfc3dSmrg		if (putchar(c) == EOF) {
223*1debfc3dSmrg		    return EOF;
224*1debfc3dSmrg		}
225*1debfc3dSmrg	    }
226*1debfc3dSmrg	    return c;
227*1debfc3dSmrg	}
228*1debfc3dSmrg    };
229*1debfc3dSmrg
230*1debfc3dSmrg    int main()
231*1debfc3dSmrg    {
232*1debfc3dSmrg	// create special output buffer
233*1debfc3dSmrg	outbuf ob;
234*1debfc3dSmrg	// initialize output stream with that output buffer
235*1debfc3dSmrg	std::ostream out(&amp;ob);
236*1debfc3dSmrg
237*1debfc3dSmrg	out &lt;&lt; "31 hexadecimal: "
238*1debfc3dSmrg	    &lt;&lt; std::hex &lt;&lt; 31 &lt;&lt; std::endl;
239*1debfc3dSmrg	return 0;
240*1debfc3dSmrg    }
241*1debfc3dSmrg   </programlisting>
242*1debfc3dSmrg   <para>Try it yourself!  More examples can be found in 3.1.x code, in
243*1debfc3dSmrg      <filename>include/ext/*_filebuf.h</filename>, and in the article
244*1debfc3dSmrg      <link xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="http://gabisoft.free.fr/articles/fltrsbf1.html">Filtering
245*1debfc3dSmrg      Streambufs</link>
246*1debfc3dSmrg      by James Kanze.
247*1debfc3dSmrg   </para>
248*1debfc3dSmrg
249*1debfc3dSmrg  </section>
250*1debfc3dSmrg
251*1debfc3dSmrg  <section xml:id="io.streambuf.buffering" xreflabel="Buffering"><info><title>Buffering</title></info>
252*1debfc3dSmrg
253*1debfc3dSmrg   <para>First, are you sure that you understand buffering?  Particularly
254*1debfc3dSmrg      the fact that C++ may not, in fact, have anything to do with it?
255*1debfc3dSmrg   </para>
256*1debfc3dSmrg   <para>The rules for buffering can be a little odd, but they aren't any
257*1debfc3dSmrg      different from those of C.  (Maybe that's why they can be a bit
258*1debfc3dSmrg      odd.)  Many people think that writing a newline to an output
259*1debfc3dSmrg      stream automatically flushes the output buffer.  This is true only
260*1debfc3dSmrg      when the output stream is, in fact, a terminal and not a file
261*1debfc3dSmrg      or some other device -- and <emphasis>that</emphasis> may not even be true
262*1debfc3dSmrg      since C++ says nothing about files nor terminals.  All of that is
263*1debfc3dSmrg      system-dependent.  (The "newline-buffer-flushing only occurring
264*1debfc3dSmrg      on terminals" thing is mostly true on Unix systems, though.)
265*1debfc3dSmrg   </para>
266*1debfc3dSmrg   <para>Some people also believe that sending <code>endl</code> down an
267*1debfc3dSmrg      output stream only writes a newline.  This is incorrect; after a
268*1debfc3dSmrg      newline is written, the buffer is also flushed.  Perhaps this
269*1debfc3dSmrg      is the effect you want when writing to a screen -- get the text
270*1debfc3dSmrg      out as soon as possible, etc -- but the buffering is largely
271*1debfc3dSmrg      wasted when doing this to a file:
272*1debfc3dSmrg   </para>
273*1debfc3dSmrg   <programlisting>
274*1debfc3dSmrg   output &lt;&lt; "a line of text" &lt;&lt; endl;
275*1debfc3dSmrg   output &lt;&lt; some_data_variable &lt;&lt; endl;
276*1debfc3dSmrg   output &lt;&lt; "another line of text" &lt;&lt; endl; </programlisting>
277*1debfc3dSmrg   <para>The proper thing to do in this case to just write the data out
278*1debfc3dSmrg      and let the libraries and the system worry about the buffering.
279*1debfc3dSmrg      If you need a newline, just write a newline:
280*1debfc3dSmrg   </para>
281*1debfc3dSmrg   <programlisting>
282*1debfc3dSmrg   output &lt;&lt; "a line of text\n"
283*1debfc3dSmrg	  &lt;&lt; some_data_variable &lt;&lt; '\n'
284*1debfc3dSmrg	  &lt;&lt; "another line of text\n"; </programlisting>
285*1debfc3dSmrg   <para>I have also joined the output statements into a single statement.
286*1debfc3dSmrg      You could make the code prettier by moving the single newline to
287*1debfc3dSmrg      the start of the quoted text on the last line, for example.
288*1debfc3dSmrg   </para>
289*1debfc3dSmrg   <para>If you do need to flush the buffer above, you can send an
290*1debfc3dSmrg      <code>endl</code> if you also need a newline, or just flush the buffer
291*1debfc3dSmrg      yourself:
292*1debfc3dSmrg   </para>
293*1debfc3dSmrg   <programlisting>
294*1debfc3dSmrg   output &lt;&lt; ...... &lt;&lt; flush;    // can use std::flush manipulator
295*1debfc3dSmrg   output.flush();               // or call a member fn </programlisting>
296*1debfc3dSmrg   <para>On the other hand, there are times when writing to a file should
297*1debfc3dSmrg      be like writing to standard error; no buffering should be done
298*1debfc3dSmrg      because the data needs to appear quickly (a prime example is a
299*1debfc3dSmrg      log file for security-related information).  The way to do this is
300*1debfc3dSmrg      just to turn off the buffering <emphasis>before any I/O operations at
301*1debfc3dSmrg      all</emphasis> have been done (note that opening counts as an I/O operation):
302*1debfc3dSmrg   </para>
303*1debfc3dSmrg   <programlisting>
304*1debfc3dSmrg   std::ofstream    os;
305*1debfc3dSmrg   std::ifstream    is;
306*1debfc3dSmrg   int   i;
307*1debfc3dSmrg
308*1debfc3dSmrg   os.rdbuf()-&gt;pubsetbuf(0,0);
309*1debfc3dSmrg   is.rdbuf()-&gt;pubsetbuf(0,0);
310*1debfc3dSmrg
311*1debfc3dSmrg   os.open("/foo/bar/baz");
312*1debfc3dSmrg   is.open("/qux/quux/quuux");
313*1debfc3dSmrg   ...
314*1debfc3dSmrg   os &lt;&lt; "this data is written immediately\n";
315*1debfc3dSmrg   is &gt;&gt; i;   // and this will probably cause a disk read </programlisting>
316*1debfc3dSmrg   <para>Since all aspects of buffering are handled by a streambuf-derived
317*1debfc3dSmrg      member, it is necessary to get at that member with <code>rdbuf()</code>.
318*1debfc3dSmrg      Then the public version of <code>setbuf</code> can be called.  The
319*1debfc3dSmrg      arguments are the same as those for the Standard C I/O Library
320*1debfc3dSmrg      function (a buffer area followed by its size).
321*1debfc3dSmrg   </para>
322*1debfc3dSmrg   <para>A great deal of this is implementation-dependent.  For example,
323*1debfc3dSmrg      <code>streambuf</code> does not specify any actions for its own
324*1debfc3dSmrg      <code>setbuf()</code>-ish functions; the classes derived from
325*1debfc3dSmrg      <code>streambuf</code> each define behavior that "makes
326*1debfc3dSmrg      sense" for that class:  an argument of (0,0) turns off buffering
327*1debfc3dSmrg      for <code>filebuf</code> but does nothing at all for its siblings
328*1debfc3dSmrg      <code>stringbuf</code> and <code>strstreambuf</code>, and specifying
329*1debfc3dSmrg      anything other than (0,0) has varying effects.
330*1debfc3dSmrg      User-defined classes derived from <code>streambuf</code> can
331*1debfc3dSmrg      do whatever they want.  (For <code>filebuf</code> and arguments for
332*1debfc3dSmrg      <code>(p,s)</code> other than zeros, libstdc++ does what you'd expect:
333*1debfc3dSmrg      the first <code>s</code> bytes of <code>p</code> are used as a buffer,
334*1debfc3dSmrg      which you must allocate and deallocate.)
335*1debfc3dSmrg   </para>
336*1debfc3dSmrg   <para>A last reminder:  there are usually more buffers involved than
337*1debfc3dSmrg      just those at the language/library level.  Kernel buffers, disk
338*1debfc3dSmrg      buffers, and the like will also have an effect.  Inspecting and
339*1debfc3dSmrg      changing those are system-dependent.
340*1debfc3dSmrg   </para>
341*1debfc3dSmrg
342*1debfc3dSmrg  </section>
343*1debfc3dSmrg</section>
344*1debfc3dSmrg
345*1debfc3dSmrg<!-- Sect1 03 : Memory-based Streams -->
346*1debfc3dSmrg<section xml:id="std.io.memstreams" xreflabel="Memory Streams"><info><title>Memory Based Streams</title></info>
347*1debfc3dSmrg<?dbhtml filename="stringstreams.html"?>
348*1debfc3dSmrg
349*1debfc3dSmrg  <section xml:id="std.io.memstreams.compat" xreflabel="Compatibility strstream"><info><title>Compatibility With strstream</title></info>
350*1debfc3dSmrg
351*1debfc3dSmrg    <para>
352*1debfc3dSmrg    </para>
353*1debfc3dSmrg   <para>Stringstreams (defined in the header <code>&lt;sstream&gt;</code>)
354*1debfc3dSmrg      are in this author's opinion one of the coolest things since
355*1debfc3dSmrg      sliced time.  An example of their use is in the Received Wisdom
356*1debfc3dSmrg      section for Sect1 21 (Strings),
357*1debfc3dSmrg      <link linkend="strings.string.Cstring"> describing how to
358*1debfc3dSmrg      format strings</link>.
359*1debfc3dSmrg   </para>
360*1debfc3dSmrg   <para>The quick definition is:  they are siblings of ifstream and ofstream,
361*1debfc3dSmrg      and they do for <code>std::string</code> what their siblings do for
362*1debfc3dSmrg      files.  All that work you put into writing <code>&lt;&lt;</code> and
363*1debfc3dSmrg      <code>&gt;&gt;</code> functions for your classes now pays off
364*1debfc3dSmrg      <emphasis>again!</emphasis>  Need to format a string before passing the string
365*1debfc3dSmrg      to a function?  Send your stuff via <code>&lt;&lt;</code> to an
366*1debfc3dSmrg      ostringstream.  You've read a string as input and need to parse it?
367*1debfc3dSmrg      Initialize an istringstream with that string, and then pull pieces
368*1debfc3dSmrg      out of it with <code>&gt;&gt;</code>.  Have a stringstream and need to
369*1debfc3dSmrg      get a copy of the string inside?  Just call the <code>str()</code>
370*1debfc3dSmrg      member function.
371*1debfc3dSmrg   </para>
372*1debfc3dSmrg   <para>This only works if you've written your
373*1debfc3dSmrg      <code>&lt;&lt;</code>/<code>&gt;&gt;</code> functions correctly, though,
374*1debfc3dSmrg      and correctly means that they take istreams and ostreams as
375*1debfc3dSmrg      parameters, not i<emphasis>f</emphasis>streams and o<emphasis>f</emphasis>streams.  If they
376*1debfc3dSmrg      take the latter, then your I/O operators will work fine with
377*1debfc3dSmrg      file streams, but with nothing else -- including stringstreams.
378*1debfc3dSmrg   </para>
379*1debfc3dSmrg   <para>If you are a user of the strstream classes, you need to update
380*1debfc3dSmrg      your code.  You don't have to explicitly append <code>ends</code> to
381*1debfc3dSmrg      terminate the C-style character array, you don't have to mess with
382*1debfc3dSmrg      "freezing" functions, and you don't have to manage the
383*1debfc3dSmrg      memory yourself.  The strstreams have been officially deprecated,
384*1debfc3dSmrg      which means that 1) future revisions of the C++ Standard won't
385*1debfc3dSmrg      support them, and 2) if you use them, people will laugh at you.
386*1debfc3dSmrg   </para>
387*1debfc3dSmrg
388*1debfc3dSmrg
389*1debfc3dSmrg  </section>
390*1debfc3dSmrg</section>
391*1debfc3dSmrg
392*1debfc3dSmrg<!-- Sect1 04 : File-based Streams -->
393*1debfc3dSmrg<section xml:id="std.io.filestreams" xreflabel="File Streams"><info><title>File Based Streams</title></info>
394*1debfc3dSmrg<?dbhtml filename="fstreams.html"?>
395*1debfc3dSmrg
396*1debfc3dSmrg
397*1debfc3dSmrg  <section xml:id="std.io.filestreams.copying_a_file" xreflabel="Copying a File"><info><title>Copying a File</title></info>
398*1debfc3dSmrg
399*1debfc3dSmrg  <para>
400*1debfc3dSmrg  </para>
401*1debfc3dSmrg
402*1debfc3dSmrg   <para>So you want to copy a file quickly and easily, and most important,
403*1debfc3dSmrg      completely portably.  And since this is C++, you have an open
404*1debfc3dSmrg      ifstream (call it IN) and an open ofstream (call it OUT):
405*1debfc3dSmrg   </para>
406*1debfc3dSmrg   <programlisting>
407*1debfc3dSmrg   #include &lt;fstream&gt;
408*1debfc3dSmrg
409*1debfc3dSmrg   std::ifstream  IN ("input_file");
410*1debfc3dSmrg   std::ofstream  OUT ("output_file"); </programlisting>
411*1debfc3dSmrg   <para>Here's the easiest way to get it completely wrong:
412*1debfc3dSmrg   </para>
413*1debfc3dSmrg   <programlisting>
414*1debfc3dSmrg   OUT &lt;&lt; IN;</programlisting>
415*1debfc3dSmrg   <para>For those of you who don't already know why this doesn't work
416*1debfc3dSmrg      (probably from having done it before), I invite you to quickly
417*1debfc3dSmrg      create a simple text file called "input_file" containing
418*1debfc3dSmrg      the sentence
419*1debfc3dSmrg   </para>
420*1debfc3dSmrg      <programlisting>
421*1debfc3dSmrg      The quick brown fox jumped over the lazy dog.</programlisting>
422*1debfc3dSmrg   <para>surrounded by blank lines.  Code it up and try it.  The contents
423*1debfc3dSmrg      of "output_file" may surprise you.
424*1debfc3dSmrg   </para>
425*1debfc3dSmrg   <para>Seriously, go do it.  Get surprised, then come back.  It's worth it.
426*1debfc3dSmrg   </para>
427*1debfc3dSmrg   <para>The thing to remember is that the <code>basic_[io]stream</code> classes
428*1debfc3dSmrg      handle formatting, nothing else.  In particular, they break up on
429*1debfc3dSmrg      whitespace.  The actual reading, writing, and storing of data is
430*1debfc3dSmrg      handled by the <code>basic_streambuf</code> family.  Fortunately, the
431*1debfc3dSmrg      <code>operator&lt;&lt;</code> is overloaded to take an ostream and
432*1debfc3dSmrg      a pointer-to-streambuf, in order to help with just this kind of
433*1debfc3dSmrg      "dump the data verbatim" situation.
434*1debfc3dSmrg   </para>
435*1debfc3dSmrg   <para>Why a <emphasis>pointer</emphasis> to streambuf and not just a streambuf?  Well,
436*1debfc3dSmrg      the [io]streams hold pointers (or references, depending on the
437*1debfc3dSmrg      implementation) to their buffers, not the actual
438*1debfc3dSmrg      buffers.  This allows polymorphic behavior on the chapter of the buffers
439*1debfc3dSmrg      as well as the streams themselves.  The pointer is easily retrieved
440*1debfc3dSmrg      using the <code>rdbuf()</code> member function.  Therefore, the easiest
441*1debfc3dSmrg      way to copy the file is:
442*1debfc3dSmrg   </para>
443*1debfc3dSmrg   <programlisting>
444*1debfc3dSmrg   OUT &lt;&lt; IN.rdbuf();</programlisting>
445*1debfc3dSmrg   <para>So what <emphasis>was</emphasis> happening with OUT&lt;&lt;IN?  Undefined
446*1debfc3dSmrg      behavior, since that particular &lt;&lt; isn't defined by the Standard.
447*1debfc3dSmrg      I have seen instances where it is implemented, but the character
448*1debfc3dSmrg      extraction process removes all the whitespace, leaving you with no
449*1debfc3dSmrg      blank lines and only "Thequickbrownfox...".  With
450*1debfc3dSmrg      libraries that do not define that operator, IN (or one of IN's
451*1debfc3dSmrg      member pointers) sometimes gets converted to a void*, and the output
452*1debfc3dSmrg      file then contains a perfect text representation of a hexadecimal
453*1debfc3dSmrg      address (quite a big surprise).  Others don't compile at all.
454*1debfc3dSmrg   </para>
455*1debfc3dSmrg   <para>Also note that none of this is specific to o<emphasis>*f*</emphasis>streams.
456*1debfc3dSmrg      The operators shown above are all defined in the parent
457*1debfc3dSmrg      basic_ostream class and are therefore available with all possible
458*1debfc3dSmrg      descendants.
459*1debfc3dSmrg   </para>
460*1debfc3dSmrg
461*1debfc3dSmrg  </section>
462*1debfc3dSmrg
463*1debfc3dSmrg  <section xml:id="std.io.filestreams.binary" xreflabel="Binary Input and Output"><info><title>Binary Input and Output</title></info>
464*1debfc3dSmrg
465*1debfc3dSmrg    <para>
466*1debfc3dSmrg    </para>
467*1debfc3dSmrg   <para>The first and most important thing to remember about binary I/O is
468*1debfc3dSmrg      that opening a file with <code>ios::binary</code> is not, repeat
469*1debfc3dSmrg      <emphasis>not</emphasis>, the only thing you have to do.  It is not a silver
470*1debfc3dSmrg      bullet, and will not allow you to use the <code>&lt;&lt;/&gt;&gt;</code>
471*1debfc3dSmrg      operators of the normal fstreams to do binary I/O.
472*1debfc3dSmrg   </para>
473*1debfc3dSmrg   <para>Sorry.  Them's the breaks.
474*1debfc3dSmrg   </para>
475*1debfc3dSmrg   <para>This isn't going to try and be a complete tutorial on reading and
476*1debfc3dSmrg      writing binary files (because "binary"
477*1debfc3dSmrg      covers a lot of ground), but we will try and clear
478*1debfc3dSmrg      up a couple of misconceptions and common errors.
479*1debfc3dSmrg   </para>
480*1debfc3dSmrg   <para>First, <code>ios::binary</code> has exactly one defined effect, no more
481*1debfc3dSmrg      and no less.  Normal text mode has to be concerned with the newline
482*1debfc3dSmrg      characters, and the runtime system will translate between (for
483*1debfc3dSmrg      example) '\n' and the appropriate end-of-line sequence (LF on Unix,
484*1debfc3dSmrg      CRLF on DOS, CR on Macintosh, etc).  (There are other things that
485*1debfc3dSmrg      normal mode does, but that's the most obvious.)  Opening a file in
486*1debfc3dSmrg      binary mode disables this conversion, so reading a CRLF sequence
487*1debfc3dSmrg      under Windows won't accidentally get mapped to a '\n' character, etc.
488*1debfc3dSmrg      Binary mode is not supposed to suddenly give you a bitstream, and
489*1debfc3dSmrg      if it is doing so in your program then you've discovered a bug in
490*1debfc3dSmrg      your vendor's compiler (or some other chapter of the C++ implementation,
491*1debfc3dSmrg      possibly the runtime system).
492*1debfc3dSmrg   </para>
493*1debfc3dSmrg   <para>Second, using <code>&lt;&lt;</code> to write and <code>&gt;&gt;</code> to
494*1debfc3dSmrg      read isn't going to work with the standard file stream classes, even
495*1debfc3dSmrg      if you use <code>skipws</code> during reading.  Why not?  Because
496*1debfc3dSmrg      ifstream and ofstream exist for the purpose of <emphasis>formatting</emphasis>,
497*1debfc3dSmrg      not reading and writing.  Their job is to interpret the data into
498*1debfc3dSmrg      text characters, and that's exactly what you don't want to happen
499*1debfc3dSmrg      during binary I/O.
500*1debfc3dSmrg   </para>
501*1debfc3dSmrg   <para>Third, using the <code>get()</code> and <code>put()/write()</code> member
502*1debfc3dSmrg      functions still aren't guaranteed to help you.  These are
503*1debfc3dSmrg      "unformatted" I/O functions, but still character-based.
504*1debfc3dSmrg      (This may or may not be what you want, see below.)
505*1debfc3dSmrg   </para>
506*1debfc3dSmrg   <para>Notice how all the problems here are due to the inappropriate use
507*1debfc3dSmrg      of <emphasis>formatting</emphasis> functions and classes to perform something
508*1debfc3dSmrg      which <emphasis>requires</emphasis> that formatting not be done?  There are a
509*1debfc3dSmrg      seemingly infinite number of solutions, and a few are listed here:
510*1debfc3dSmrg   </para>
511*1debfc3dSmrg   <itemizedlist>
512*1debfc3dSmrg      <listitem>
513*1debfc3dSmrg	<para><quote>Derive your own fstream-type classes and write your own
514*1debfc3dSmrg	  &lt;&lt;/&gt;&gt; operators to do binary I/O on whatever data
515*1debfc3dSmrg	  types you're using.</quote>
516*1debfc3dSmrg	</para>
517*1debfc3dSmrg	<para>
518*1debfc3dSmrg	  This is a Bad Thing, because while
519*1debfc3dSmrg	  the compiler would probably be just fine with it, other humans
520*1debfc3dSmrg	  are going to be confused.  The overloaded bitshift operators
521*1debfc3dSmrg	  have a well-defined meaning (formatting), and this breaks it.
522*1debfc3dSmrg	</para>
523*1debfc3dSmrg      </listitem>
524*1debfc3dSmrg      <listitem>
525*1debfc3dSmrg	<para>
526*1debfc3dSmrg	  <quote>Build the file structure in memory, then
527*1debfc3dSmrg	  <code>mmap()</code> the file and copy the
528*1debfc3dSmrg	  structure.
529*1debfc3dSmrg	</quote>
530*1debfc3dSmrg	</para>
531*1debfc3dSmrg	<para>
532*1debfc3dSmrg	  Well, this is easy to make work, and easy to break, and is
533*1debfc3dSmrg	  pretty equivalent to using <code>::read()</code> and
534*1debfc3dSmrg	  <code>::write()</code> directly, and makes no use of the
535*1debfc3dSmrg	  iostream library at all...
536*1debfc3dSmrg	  </para>
537*1debfc3dSmrg      </listitem>
538*1debfc3dSmrg      <listitem>
539*1debfc3dSmrg	<para>
540*1debfc3dSmrg	  <quote>Use streambufs, that's what they're there for.</quote>
541*1debfc3dSmrg	</para>
542*1debfc3dSmrg	<para>
543*1debfc3dSmrg	  While not trivial for the beginner, this is the best of all
544*1debfc3dSmrg	  solutions.  The streambuf/filebuf layer is the layer that is
545*1debfc3dSmrg	  responsible for actual I/O.  If you want to use the C++
546*1debfc3dSmrg	  library for binary I/O, this is where you start.
547*1debfc3dSmrg	</para>
548*1debfc3dSmrg      </listitem>
549*1debfc3dSmrg   </itemizedlist>
550*1debfc3dSmrg   <para>How to go about using streambufs is a bit beyond the scope of this
551*1debfc3dSmrg      document (at least for now), but while streambufs go a long way,
552*1debfc3dSmrg      they still leave a couple of things up to you, the programmer.
553*1debfc3dSmrg      As an example, byte ordering is completely between you and the
554*1debfc3dSmrg      operating system, and you have to handle it yourself.
555*1debfc3dSmrg   </para>
556*1debfc3dSmrg   <para>Deriving a streambuf or filebuf
557*1debfc3dSmrg      class from the standard ones, one that is specific to your data
558*1debfc3dSmrg      types (or an abstraction thereof) is probably a good idea, and
559*1debfc3dSmrg      lots of examples exist in journals and on Usenet.  Using the
560*1debfc3dSmrg      standard filebufs directly (either by declaring your own or by
561*1debfc3dSmrg      using the pointer returned from an fstream's <code>rdbuf()</code>)
562*1debfc3dSmrg      is certainly feasible as well.
563*1debfc3dSmrg   </para>
564*1debfc3dSmrg   <para>One area that causes problems is trying to do bit-by-bit operations
565*1debfc3dSmrg      with filebufs.  C++ is no different from C in this respect:  I/O
566*1debfc3dSmrg      must be done at the byte level.  If you're trying to read or write
567*1debfc3dSmrg      a few bits at a time, you're going about it the wrong way.  You
568*1debfc3dSmrg      must read/write an integral number of bytes and then process the
569*1debfc3dSmrg      bytes.  (For example, the streambuf functions take and return
570*1debfc3dSmrg      variables of type <code>int_type</code>.)
571*1debfc3dSmrg   </para>
572*1debfc3dSmrg   <para>Another area of problems is opening text files in binary mode.
573*1debfc3dSmrg      Generally, binary mode is intended for binary files, and opening
574*1debfc3dSmrg      text files in binary mode means that you now have to deal with all of
575*1debfc3dSmrg      those end-of-line and end-of-file problems that we mentioned before.
576*1debfc3dSmrg   </para>
577*1debfc3dSmrg   <para>
578*1debfc3dSmrg      An instructive thread from comp.lang.c++.moderated delved off into
579*1debfc3dSmrg      this topic starting more or less at
580*1debfc3dSmrg      <link xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="https://groups.google.com/forum/#!topic/comp.std.c++/D4e0q9eVSoc">this post</link>
581*1debfc3dSmrg      and continuing to the end of the thread. (The subject heading is "binary iostreams" on both comp.std.c++
582*1debfc3dSmrg      and comp.lang.c++.moderated.) Take special note of the replies by James Kanze and Dietmar Kühl.
583*1debfc3dSmrg   </para>
584*1debfc3dSmrg    <para>Briefly, the problems of byte ordering and type sizes mean that
585*1debfc3dSmrg      the unformatted functions like <code>ostream::put()</code> and
586*1debfc3dSmrg      <code>istream::get()</code> cannot safely be used to communicate
587*1debfc3dSmrg      between arbitrary programs, or across a network, or from one
588*1debfc3dSmrg      invocation of a program to another invocation of the same program
589*1debfc3dSmrg      on a different platform, etc.
590*1debfc3dSmrg   </para>
591*1debfc3dSmrg </section>
592*1debfc3dSmrg
593*1debfc3dSmrg</section>
594*1debfc3dSmrg
595*1debfc3dSmrg<!-- Sect1 03 : Interacting with C -->
596*1debfc3dSmrg<section xml:id="std.io.c" xreflabel="Interacting with C"><info><title>Interacting with C</title></info>
597*1debfc3dSmrg<?dbhtml filename="io_and_c.html"?>
598*1debfc3dSmrg
599*1debfc3dSmrg
600*1debfc3dSmrg
601*1debfc3dSmrg  <section xml:id="std.io.c.FILE" xreflabel="Using FILE* and file descriptors"><info><title>Using FILE* and file descriptors</title></info>
602*1debfc3dSmrg
603*1debfc3dSmrg    <para>
604*1debfc3dSmrg      See the <link linkend="manual.ext.io">extensions</link> for using
605*1debfc3dSmrg      <type>FILE</type> and <type>file descriptors</type> with
606*1debfc3dSmrg      <classname>ofstream</classname> and
607*1debfc3dSmrg      <classname>ifstream</classname>.
608*1debfc3dSmrg    </para>
609*1debfc3dSmrg  </section>
610*1debfc3dSmrg
611*1debfc3dSmrg  <section xml:id="std.io.c.sync" xreflabel="Performance Issues"><info><title>Performance</title></info>
612*1debfc3dSmrg
613*1debfc3dSmrg    <para>
614*1debfc3dSmrg      Pathetic Performance? Ditch C.
615*1debfc3dSmrg    </para>
616*1debfc3dSmrg   <para>It sounds like a flame on C, but it isn't.  Really.  Calm down.
617*1debfc3dSmrg      I'm just saying it to get your attention.
618*1debfc3dSmrg   </para>
619*1debfc3dSmrg   <para>Because the C++ library includes the C library, both C-style and
620*1debfc3dSmrg      C++-style I/O have to work at the same time.  For example:
621*1debfc3dSmrg   </para>
622*1debfc3dSmrg   <programlisting>
623*1debfc3dSmrg     #include &lt;iostream&gt;
624*1debfc3dSmrg     #include &lt;cstdio&gt;
625*1debfc3dSmrg
626*1debfc3dSmrg     std::cout &lt;&lt; "Hel";
627*1debfc3dSmrg     std::printf ("lo, worl");
628*1debfc3dSmrg     std::cout &lt;&lt; "d!\n";
629*1debfc3dSmrg   </programlisting>
630*1debfc3dSmrg   <para>This must do what you think it does.
631*1debfc3dSmrg   </para>
632*1debfc3dSmrg   <para>Alert members of the audience will immediately notice that buffering
633*1debfc3dSmrg      is going to make a hash of the output unless special steps are taken.
634*1debfc3dSmrg   </para>
635*1debfc3dSmrg   <para>The special steps taken by libstdc++, at least for version 3.0,
636*1debfc3dSmrg      involve doing very little buffering for the standard streams, leaving
637*1debfc3dSmrg      most of the buffering to the underlying C library.  (This kind of
638*1debfc3dSmrg      thing is tricky to get right.)
639*1debfc3dSmrg      The upside is that correctness is ensured.  The downside is that
640*1debfc3dSmrg      writing through <code>cout</code> can quite easily lead to awful
641*1debfc3dSmrg      performance when the C++ I/O library is layered on top of the C I/O
642*1debfc3dSmrg      library (as it is for 3.0 by default).  Some patches have been applied
643*1debfc3dSmrg      which improve the situation for 3.1.
644*1debfc3dSmrg   </para>
645*1debfc3dSmrg   <para>However, the C and C++ standard streams only need to be kept in sync
646*1debfc3dSmrg      when both libraries' facilities are in use.  If your program only uses
647*1debfc3dSmrg      C++ I/O, then there's no need to sync with the C streams.  The right
648*1debfc3dSmrg      thing to do in this case is to call
649*1debfc3dSmrg   </para>
650*1debfc3dSmrg   <programlisting>
651*1debfc3dSmrg     #include <emphasis>any of the I/O headers such as ios, iostream, etc</emphasis>
652*1debfc3dSmrg
653*1debfc3dSmrg     std::ios::sync_with_stdio(false);
654*1debfc3dSmrg   </programlisting>
655*1debfc3dSmrg   <para>You must do this before performing any I/O via the C++ stream objects.
656*1debfc3dSmrg      Once you call this, the C++ streams will operate independently of the
657*1debfc3dSmrg      (unused) C streams.  For GCC 3.x, this means that <code>cout</code> and
658*1debfc3dSmrg      company will become fully buffered on their own.
659*1debfc3dSmrg   </para>
660*1debfc3dSmrg   <para>Note, by the way, that the synchronization requirement only applies to
661*1debfc3dSmrg      the standard streams (<code>cin</code>, <code>cout</code>,
662*1debfc3dSmrg      <code>cerr</code>,
663*1debfc3dSmrg      <code>clog</code>, and their wide-character counterparts).  File stream
664*1debfc3dSmrg      objects that you declare yourself have no such requirement and are fully
665*1debfc3dSmrg      buffered.
666*1debfc3dSmrg   </para>
667*1debfc3dSmrg
668*1debfc3dSmrg
669*1debfc3dSmrg  </section>
670*1debfc3dSmrg</section>
671*1debfc3dSmrg
672*1debfc3dSmrg</chapter>
673