📄 c++的iostream标准库介绍.htm
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3c.org/TR/1999/REC-html401-19991224/loose.dtd">
<!-- saved from url=(0063)http://www.stlchina.org/twiki/bin/view.pl/Main/STLIOStreamIntro -->
<HTML lang=en-us xml:lang="en-us"
xmlns="http://www.w3.org/1999/xhtml"><HEAD><TITLE>C++的iostream标准库介绍 -- STLIOStreamIntro</TITLE>
<META http-equiv=Content-Type content="text/html; charset=gb2312"><LINK
href="/twiki/pub/Main/WebPreferences/favicon.ico" type=image/x-icon
rel=icon><LINK href="/twiki/pub/Main/WebPreferences/favicon.ico"
type=image/x-icon rel="shortcut icon"><LINK title="RSS Feed"
href="/twiki/bin/view.pl/Main/WebRss" type=application/rss+xml rel=alternate><!--/closing tag 'solves' IE selection bug-->
<SCRIPT src="C++的iostream标准库介绍.files/twiki.js" type=text/javascript></SCRIPT>
<SCRIPT src="C++的iostream标准库介绍.files/pattern.js" type=text/javascript></SCRIPT>
<SCRIPT type=text/javascript>//<![CDATA[ function launchWindow(inWeb, inTopic) { return launchTheWindow("/twiki/bin/view.pl/", inWeb, inTopic, "", "viewplain" ); }//]]></SCRIPT>
<SCRIPT src="C++的iostream标准库介绍.files/twist.js" type=text/javascript></SCRIPT>
<STYLE type=text/css media=all>@import url( /twiki/pub/TWiki/TwistyContrib/twist.css );
</STYLE>
<STYLE type=text/css media=all>#patternTopBar {
OVERFLOW: hidden; HEIGHT: 64px
}
#patternClearHeaderCenter {
OVERFLOW: hidden; HEIGHT: 64px
}
#patternClearHeaderLeft {
OVERFLOW: hidden; HEIGHT: 64px
}
#patternClearHeaderRight {
OVERFLOW: hidden; HEIGHT: 64px
}
#patternTopBarContentsOuter {
OVERFLOW: hidden; HEIGHT: 64px
}
#patternOuter {
MARGIN-LEFT: 12em
}
#patternLeftBar {
MARGIN-LEFT: -12em; WIDTH: 12em
}
</STYLE>
<STYLE type=text/css media=all>@import url( /twiki/pub/TWiki/PatternSkin/layout.css );
@import url( /twiki/pub/TWiki/PatternSkin/style.css );
@import url( /twiki/pub/TWiki/PatternSkin/colors.css );
</STYLE>
<!--[if IE]>
<STYLE type=text/css>PRE {
OVERFLOW-Y: hidden; OVERFLOW-X: auto; ; PADDING-BOTTOM: expression(this.scrollWidth > this.offsetWidth ? 16 : 0)
}
#patternLeftBarContents LI {
TEXT-OVERFLOW: ellipsis
}
</STYLE>
<![endif]-->
<STYLE type=text/css media=all>.twikiInputField {
BACKGROUND-IMAGE: url(/twiki/pub/TWiki/PatternSkin/background_input.gif); BACKGROUND-REPEAT: repeat-x
}
.twikiButton {
BACKGROUND-IMAGE: url(/twiki/pub/TWiki/PatternSkin/background_button.gif); BACKGROUND-REPEAT: repeat-x
}
.patternToolBarButtons .patternButton A {
}
.twikiSubmit {
BACKGROUND-IMAGE: url(/twiki/pub/TWiki/PatternSkin/background_submit.gif); BACKGROUND-REPEAT: repeat-x
}
.twikiToc LI {
BACKGROUND-IMAGE: url(/twiki/pub/TWiki/PatternSkin/bullet-down.gif); BACKGROUND-REPEAT: no-repeat
}
#patternLeftBar .patternWebIndicator {
BACKGROUND-COLOR: #ffefa6
}
.patternBookView .twikiTopRow {
BACKGROUND-COLOR: #ffefa6
}
#patternTopBar {
BACKGROUND-IMAGE: url(/twiki/pub/TWiki/PatternSkin/TWiki_header.gif); BACKGROUND-REPEAT: no-repeat
}
#patternTopBar {
BACKGROUND-COLOR: #ffffff
}
.patternBookView {
BORDER-LEFT-COLOR: #ffefa6; BORDER-BOTTOM-COLOR: #ffefa6; BORDER-TOP-COLOR: #ffefa6; BORDER-RIGHT-COLOR: #ffefa6
}
#twikiLogin {
BACKGROUND-IMAGE: url(/twiki/pub/TWiki/PatternSkin/gradient_blue.gif); BACKGROUND-REPEAT: repeat-x
}
#patternLeftBarContents .tipsOfTheDayContents {
BACKGROUND-IMAGE: url(/twiki/pub/TWiki/PatternSkin/gradient_blue.gif); BACKGROUND-REPEAT: repeat-x
}
#patternOuter {
BACKGROUND-IMAGE: url(/twiki/pub/TWiki/PatternSkin/gradient_yellow.gif); BACKGROUND-REPEAT: repeat-x
}
.patternPrintPage #patternOuter {
BACKGROUND: #fff
}
.twikiPageForm TABLE {
BACKGROUND-IMAGE: url(/twiki/pub/TWiki/PatternSkin/form_gradient.gif); BACKGROUND-REPEAT: repeat-x
}
#patternLeftBarContents .patternLeftBarPersonal LI {
BACKGROUND-POSITION: 0px 0.5em; BACKGROUND-IMAGE: url(/twiki/pub/TWiki/PatternSkin/bullet-blue.gif); BACKGROUND-REPEAT: no-repeat
}
.patternPreviewPage #patternMain {
BACKGROUND-IMAGE: url(/twiki/pub/TWiki/PreviewBackground/preview2bg.gif)
}
</STYLE>
<STYLE type=text/css media=all></STYLE>
<META content="MSHTML 6.00.2900.3059" name=GENERATOR></HEAD>
<BODY class=patternViewPage><A name=PageTop></A>
<DIV id=patternScreen>
<DIV id=patternPageShadow>
<DIV id=patternPage>
<DIV id=patternWrapper>
<DIV id=patternOuter>
<DIV id=patternFloatWrap>
<DIV id=patternMain>
<DIV id=patternClearHeaderCenter></DIV>
<DIV id=patternMainContents>
<DIV class=patternTop>
<DIV class=patternToolBar>
<DIV class=patternToolBarButtons><SPAN class=patternButton><A
title="Edit this topic text" accessKey=E
href="http://www.stlchina.org/twiki/bin/edit.pl/Main/STLIOStreamIntro?t=1174464209"
rel=nofollow><SPAN class=twikiAccessKey>E</SPAN>dit</A></SPAN><SPAN
class=patternButton><A title="Attach an image or document to this topic"
accessKey=A
href="http://www.stlchina.org/twiki/bin/attach.pl/Main/STLIOStreamIntro"
rel=nofollow><SPAN class=twikiAccessKey>A</SPAN>ttach</A></SPAN><SPAN
class=patternButton><A title="Printable version of this topic" accessKey=P
href="http://www.stlchina.org/twiki/bin/view.pl/Main/STLIOStreamIntro?template=viewprint"
rel=nofollow><SPAN class=twikiAccessKey>P</SPAN>rintable</A></SPAN> </DIV></DIV><!-- /patternToolBar-->
<DIV class=patternToolBarBottom></DIV></DIV><!-- /patternTop-->
<DIV class=patternHomePath><SPAN class=twikiRight><SPAN class=patternRevInfo>r1
- 17 Jul 2005 - 12:18:08 - <A class=twikiLink
href="http://www.stlchina.org/twiki/bin/view.pl/Main/WinterWen">WinterWen</A></SPAN></SPAN><SPAN
class=patternHomePathTitle>You are here: </SPAN><A class=twikiLink
href="http://www.stlchina.org/twiki/bin/view.pl/Main/WebHome">TWiki</A><SPAN
class=twikiSeparator> > </SPAN><A
href="http://www.stlchina.org/twiki/bin/view.pl/Main/WebHome"><IMG
style="BACKGROUND-COLOR: #ffefa6" height=13 alt=""
src="C++的iostream标准库介绍.files/web-bg-small.gif" width=13 border=0> Main Web</A>
<SPAN class=twikiSeparator> > </SPAN><A class=twikiLink
href="http://www.stlchina.org/twiki/bin/view.pl/Main/WebLeftBar">WebLeftBar</A>
> <A class=twikiLink
href="http://www.stlchina.org/twiki/bin/view.pl/Main/STLChina">STLChina</A><SPAN
class=twikiSeparator> > </SPAN><SPAN
title=Main.STLIOStreamIntro>STLIOStreamIntro</SPAN><BR class=twikiClear></DIV><!-- /patternHomePath-->
<DIV class=twikiBeforeText></DIV>
<DIV class=patternContent>
<DIV class=patternTopic>
<H2><A name=C++的iostream标准库介绍></A>C++的iostream标准库介绍 </H2>作者:管宁
<DIV class=twikiToc>
<UL>
<LI><A
href="http://www.stlchina.org/twiki/bin/view.pl/Main/STLIOStreamIntro#C++的iostream标准库介绍">C++的iostream标准库介绍</A>
<UL>
<LI><A
href="http://www.stlchina.org/twiki/bin/view.pl/Main/STLIOStreamIntro#0 为什么需要iostream">0
为什么需要iostream</A>
<LI><A
href="http://www.stlchina.org/twiki/bin/view.pl/Main/STLIOStreamIntro#1 iostream: istream 和 ostream">1
iostream: istream 和 ostream</A>
<LI><A
href="http://www.stlchina.org/twiki/bin/view.pl/Main/STLIOStreamIntro#2 fstream: ifstream 和 ofstream">2
fstream: ifstream 和 ofstream</A>
<LI><A
href="http://www.stlchina.org/twiki/bin/view.pl/Main/STLIOStreamIntro#3 strstream: ostrstream 和 istrs">3
strstream: ostrstream 和 istrstream</A>
<LI><A
href="http://www.stlchina.org/twiki/bin/view.pl/Main/STLIOStreamIntro#4 stringstream">4
stringstream</A>
<LI><A
href="http://www.stlchina.org/twiki/bin/view.pl/Main/STLIOStreamIntro#5 io_state 输入/输出的状态标志">5
io_state 输入/输出的状态标志</A> </LI></UL></LI></UL></DIV>
<H3><A name="0 为什么需要iostream"></A>0 为什么需要iostream
</H3>我们从一开始就一直在利用C++的输入输出在做着各种练习,输入输出是由iostream库提供的,所以讨论此标准库是有必要的,它与C语言的
stdio库不同,它从一开始就是用多重继承与虚拟继承实现的面向对象的层次结构,作为一个c++的标准库组件提供给程序员使用。
<P> iostream为内置类型类型对象提供了输入输出支持,同时也支持文件的输入输出,类的设计者可以通过对iostream库的扩展,来支持自定义类型的输入输出操作。
<P> 为什么说要扩展才能提供支持呢?我们来一个示例。
<DIV class=fragment><PRE><FONT color=navy>#include</FONT> <stdio.h>
<FONT color=navy>#include</FONT> <iostream>
<FONT color=brown>using</FONT> <FONT color=brown>namespace</FONT> std;
<FONT color=brown>class</FONT> Test
{
<FONT color=brown>public</FONT>:
Test(<FONT color=brown>int</FONT> a=0,<FONT color=brown>int</FONT> b=0)
{
Test::a=a;
Test::b=b;
}
<FONT color=brown>int</FONT> a;
<FONT color=brown>int</FONT> b;
};
<FONT color=brown>int</FONT> main()
{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -