86 lines
5.7 KiB
HTML
86 lines
5.7 KiB
HTML
<!--
|
|
Licensed to the Apache Software Foundation (ASF) under one or more
|
|
contributor license agreements. See the NOTICE file distributed
|
|
with this work for additional information regarding copyright
|
|
ownership. The ASF licenses this file to you under the Apache
|
|
License, Version 2.0 (the License); you may not use this file
|
|
except in compliance with the License. You may obtain a copy of
|
|
the License at
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
implied. See the License for the specific language governing
|
|
permissions and limitations under the License.
|
|
|
|
Copyright 1999-2007 Rogue Wave Software, Inc.
|
|
-->
|
|
|
|
<HTML>
|
|
<HEAD>
|
|
<TITLE>The Standard Iostreams</TITLE>
|
|
<LINK REL=StyleSheet HREF="../rw.css" TYPE="text/css" TITLE="Apache stdcxx Stylesheet"></HEAD>
|
|
<BODY BGCOLOR=#FFFFFF>
|
|
<A HREF="27.html"><IMG SRC="images/bprev.gif" WIDTH=20 HEIGHT=21 ALT="Previous file" BORDER=O></A><A HREF="noframes.html"><IMG SRC="images/btop.gif" WIDTH=56 HEIGHT=21 ALT="Top of Document" BORDER=O></A><A HREF="booktoc.html"><IMG SRC="images/btoc.gif" WIDTH=56 HEIGHT=21 ALT="Contents" BORDER=O></A><A HREF="tindex.html"><IMG SRC="images/bindex.gif" WIDTH=56 HEIGHT=21 ALT="Index page" BORDER=O></A><A HREF="27-2.html"><IMG SRC="images/bnext.gif" WIDTH=25 HEIGHT=21 ALT="Next file" BORDER=O></A><DIV CLASS="DOCUMENTNAME"><B>Apache C++ Standard Library User's Guide</B></DIV>
|
|
<H2>27.1 The Standard Iostreams</H2>
|
|
<A NAME="idx614"><!></A>
|
|
<P>The C++ Standard Library includes templates for data stream input/output. Before the current ANSI/ISO standard, most C++ compilers were delivered with a class library commonly known as the <I>iostreams </I>library. In this manual, we refer to this library as the <I>traditional iostreams</I>, in contrast to the<I> standard iostreams</I> that are now part of the ANSI/ISO C++ Standard Library. The standard iostreams are to some extent compatible with the traditional iostreams, in that the overall architecture and the most commonly used interfaces are retained. <A HREF="45.html">Chapter 45</A> describes the incompatibilities in greater detail.</P>
|
|
<A NAME="idx615"><!></A>
|
|
<P>We can compare the standard iostreams not only with the traditional C++ iostreams library, but also with the I/O support in the Standard C Library. Many former C programmers still prefer the input/output functions offered by the C library, often referred to as <I>C stdio</I>. Their familiarity with the C library is justification enough for using the C stdio instead of C++ iostreams, but there are other reasons as well. For example, calls to the C functions <SAMP>printf()</SAMP> and <SAMP>scanf()</SAMP> are admittedly more concise with C stdio. However, C stdio has drawbacks, too, such as type insecurity and inability to extend consistently for user-defined classes. We'll discuss these in more detail in the following sections.</P>
|
|
<A NAME="idx616"><!></A>
|
|
<A NAME="2711"><H3>27.1.1 Type Safety</H3></A>
|
|
<A NAME="idx617"><!></A>
|
|
<P>Let us compare a call to stdio functions with the use of standard iostreams. The stdio call reads as follows:</P>
|
|
|
|
<UL><PRE>
|
|
int i = 25;
|
|
char name[50] = "Janakiraman";
|
|
fprintf(stdout, "%d %s", i, name);
|
|
</PRE></UL>
|
|
<P>It correctly prints: <SAMP>25 Janakiraman</SAMP>.</P>
|
|
<P>But what if we inadvertently switch the arguments to <SAMP>fprintf()</SAMP>? The error is detected no sooner than run time. Anything can happen, from peculiar output to a system crash. This is not the case with the standard iostreams:</P>
|
|
|
|
<UL><PRE>
|
|
std::cout << i << ' ' << name << '\n';
|
|
</PRE></UL>
|
|
<P>Since there are overloaded versions of the shift operator <SAMP>operator<<()</SAMP>, the right operator is always called. The expression <SAMP>std::cout << i </SAMP>calls <SAMP>std::ostream::operator<<(int)</SAMP>, and <SAMP>std::cout << name</SAMP> calls <SAMP>std::ostream::operator<< (std::ostream&, const char*)</SAMP>. Hence, the standard iostreams are typesafe.</P>
|
|
<A NAME="idx618"><!></A>
|
|
<A NAME="2712"><H3>27.1.2 Extensibility to New Types</H3></A>
|
|
<A NAME="idx619"><!></A>
|
|
<P>Another advantage of the standard iostreams is that user-defined types can be made to fit in seamlessly. Consider a type <SAMP>Pair</SAMP> that we want to print:</P>
|
|
|
|
<UL><PRE>
|
|
struct Pair { int x; string y; }
|
|
</PRE></UL>
|
|
<P>All we need to do is overload <SAMP>operator<<()</SAMP> for this new type <SAMP>Pair,</SAMP> and we can output pairs this way:</P>
|
|
|
|
<UL><PRE>
|
|
Pair p = { 5, "May" };
|
|
std::cout << p;
|
|
</PRE></UL>
|
|
<P>The corresponding <SAMP>operator<<()</SAMP> can be implemented as:</P>
|
|
|
|
<UL><PRE>
|
|
std::ostream&
|
|
operator<<(std::ostream& o, const Pair& p)
|
|
{ return o << p.x << ' ' << p.y; }
|
|
</PRE></UL>
|
|
|
|
<BR>
|
|
<HR>
|
|
<A HREF="27.html"><IMG SRC="images/bprev.gif" WIDTH=20 HEIGHT=21 ALT="Previous file" BORDER=O></A><A HREF="noframes.html"><IMG SRC="images/btop.gif" WIDTH=56 HEIGHT=21 ALT="Top of Document" BORDER=O></A><A HREF="booktoc.html"><IMG SRC="images/btoc.gif" WIDTH=56 HEIGHT=21 ALT="Contents" BORDER=O></A><A HREF="tindex.html"><IMG SRC="images/bindex.gif" WIDTH=56 HEIGHT=21 ALT="Index page" BORDER=O></A><A HREF="27-2.html"><IMG SRC="images/bnext.gif" WIDTH=20 HEIGHT=21 ALT="Next file" BORDER=O></A>
|
|
|
|
<!-- Google Analytics tracking code -->
|
|
<script src="http://www.google-analytics.com/urchin.js" type="text/javascript">
|
|
</script>
|
|
<script type="text/javascript">
|
|
_uacct = "UA-1775151-1";
|
|
urchinTracker();
|
|
</script>
|
|
<!-- end of Google Analytics tracking code -->
|
|
|
|
</BODY>
|
|
</HTML>
|