📄 c++的iostream标准库介绍.htm
字号:
{
<FONT color=brown>int</FONT> a;
cin>>a;
cout<<cin.rdstate()<<endl;
cin.clear(ios::goodbit);
cout<<cin.rdstate()<<endl;
system("<FONT color=blue>pause</FONT>");
}</PRE><PRE></PRE></DIV>通常当我们发现输入有错又需要改正的时候,使用clear()更改标记为正确后,同时也需要使用get()成员函数清除输入缓冲区,以达到重复输入的目的。
<P> 示例代码如下:
<DIV class=fragment><PRE><FONT color=navy>#include</FONT> <iostream>
<FONT color=brown>using</FONT> <FONT color=brown>namespace</FONT> std;
<FONT color=brown>int</FONT> main()
{
<FONT color=brown>int</FONT> a;
<FONT color=brown>while</FONT>(1)
{
cin>>a;
<FONT color=brown>if</FONT>(!cin)<FONT color=green>//条件可改写为cin.fail() </FONT>
{
cout<<"<FONT color=blue>输入有错!请重新输入</FONT>"<<endl;
cin.clear();
cin.get();
}
<FONT color=brown>else</FONT>
{
cout<<a;
<FONT color=brown>break</FONT>;
}
}
system("<FONT color=blue>pause</FONT>");
}</PRE><PRE></PRE></DIV> 最后再给出一个对文件流错误标记处理的例子,巩固学习,代码如下:
<DIV class=fragment><PRE><FONT color=navy>#include</FONT> <iostream>
<FONT color=navy>#include</FONT> <fstream>
<FONT color=brown>using</FONT> <FONT color=brown>namespace</FONT> std;
<FONT color=brown>int</FONT> main()
{
ifstream myfile("<FONT color=blue>c:\\1.txt</FONT>",ios_base::in,0);
<FONT color=brown>if</FONT>(myfile.fail())
{
cout<<"<FONT color=blue>文件读取失败或指定文件不存在!</FONT>"<<endl;
}
<FONT color=brown>else</FONT>
{
<FONT color=brown>char</FONT> ch;
<FONT color=brown>while</FONT>(myfile.get(ch))
{
cout<<ch;
}
<FONT color=brown>if</FONT>(myfile.eof())
{
cout<<"<FONT color=blue>文件内容已经全部读完</FONT>"<<endl;
}
<FONT color=brown>while</FONT>(myfile.get(ch))
{
cout<<ch;
}
}
system("<FONT color=blue>pause</FONT>");
}</PRE><PRE></PRE></DIV>
<P>
<P> 未完待续……
<!-- <ul><li> Set MYTITLE = C++的iostream标准库介绍</li></ul> --></P></DIV><!-- /patternTopic-->
<DIV class=twikiAfterText></DIV></DIV><!-- /patternContent--><A
name=topic-actions></A>
<DIV class=patternTopicAction><SPAN class=patternActionButtons><SPAN
class=patternButton><A title="Edit this topic text" accessKey=E
href="http://www.stlchina.org/twiki/bin/edit.pl/Main/STLIOStreamIntro?t=1174464211"
rel=nofollow><SPAN class=twikiAccessKey>E</SPAN>dit</A></SPAN><SPAN
class=twikiSeparator> | </SPAN><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
class=twikiSeparator> | </SPAN><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
class=twikiSeparator> | </SPAN><A
title="View raw text without formatting" accessKey=r
href="http://www.stlchina.org/twiki/bin/view.pl/Main/STLIOStreamIntro?raw=on"
rel=nofollow><SPAN class=twikiAccessKey>R</SPAN>aw View</A><SPAN
class=twikiSeparator> | </SPAN>Backlinks: <A
title="Search the Main Web for topics that link to here" accessKey=b
href="http://www.stlchina.org/twiki/bin/oops.pl/Main/STLIOStreamIntro?template=backlinksweb">We<SPAN
class=twikiAccessKey>b</SPAN></A>, <A
title="Search all webs for topics that link to here" accessKey=L
href="http://www.stlchina.org/twiki/bin/oops.pl/Main/STLIOStreamIntro?template=backlinksallwebs">A<SPAN
class=twikiAccessKey>l</SPAN>l Webs</A><SPAN
class=twikiSeparator> | </SPAN><SPAN class=patternRevision><A
title="View total topic history" accessKey=H
href="http://www.stlchina.org/twiki/bin/rdiff.pl/Main/STLIOStreamIntro?type=history"
rel=nofollow><SPAN class=twikiAccessKey>H</SPAN>istory</A>: r1</SPAN><SPAN
class=twikiSeparator> | </SPAN><A
title="Delete or rename this topic; set parent topic; view and compare revisions"
accessKey=M
href="http://www.stlchina.org/twiki/bin/oops.pl/Main/STLIOStreamIntro?template=oopsmore&param1=1&param2=1"
rel=nofollow><SPAN class=twikiAccessKey>M</SPAN>ore topic
actions</A></SPAN></DIV>
<DIV class=patternMoved></DIV><!-- /patternMoved--></DIV><!-- /patternMainContents--></DIV><!-- /patternMain-->
<DIV id=patternLeftBar>
<DIV id=patternClearHeaderLeft></DIV>
<DIV id=patternLeftBarContents>
<DIV class=patternWebIndicator><B><A
href="http://www.stlchina.org/twiki/bin/view.pl/Main/WebHome">Main</A></B></DIV>
<UL>
<LI><STRONG><A class=twikiLink
href="http://www.stlchina.org/twiki/bin/view.pl/Main/STLChina">STL
中文站</A></STRONG> </LI>
<LI><STRONG><A class=twikiLink
href="http://www.stlchina.org/twiki/bin/view.pl/Main/BoostChina">Boost
中文站</A></STRONG> </LI>
<LI><STRONG><A class=twikiLink
href="http://www.stlchina.org/twiki/bin/view.pl/Main/SearchEngine">搜索引擎技术</A></STRONG>
</LI>
<LI><STRONG><A href="http://www.stlchina.org/bbs/"
target=_top>STL论坛</A></STRONG> </LI>
<LI><STRONG><A class=twikiLink
href="http://www.stlchina.org/twiki/bin/view.pl/Main/ScriptProgram">脚本编程</A></STRONG>
</LI>
<LI><STRONG><A href="http://www.stlchina.org/twiki/bin/login.pl"
target=_top>管理登陆</A></STRONG> </LI></UL>
<HR>
<UL>
<LI><B>STL China</B> </LI>
<LI><A class=twikiLink
href="http://www.stlchina.org/twiki/bin/view.pl/Main/SiteMap">站内地图</A> </LI>
<LI><A class=twikiLink
href="http://www.stlchina.org/twiki/bin/view.pl/Main/WhatIsNew">最近更新</A> </LI>
<LI><A class=twikiLink
href="http://www.stlchina.org/twiki/bin/view.pl/Main/RecommendTopics">热点文章</A>
</LI>
<LI><A class=twikiLink
href="http://www.stlchina.org/twiki/bin/view.pl/Main/WebIndex">主题列表</A>
</LI></UL>
<P><!-- <ul><li> <a href="/twiki/bin/view.pl/Main/AboutUs" class="twikiLink">关于本站</a></li></ul> -->
<P>
<P>
<HR>
<UL>
<LI><STRONG>TWiki 参考</STRONG> </LI>
<LI><A class=twikiLink
href="http://www.stlchina.org/twiki/bin/view.pl/Main/InstallWiki">中文TWiki安装</A>
</LI>
<LI><A class=twikiLink
href="http://www.stlchina.org/twiki/bin/view.pl/TWiki/TWikiQickStart">TWiki
使用入门</A> </LI>
<LI><A class=twikiLink
href="http://www.stlchina.org/twiki/bin/view.pl/TWiki/TextFormattingRules">TWiki语法</A>
</LI></UL>
<P>
<HR>
<UL>
<LI><STRONG>友情链接</STRONG> </LI>
<LI><A href="http://stl.winterxy.com/" target=_top
rel=nofollow>最优秀的STL学习网站</A> </LI>
<LI><A href="http://www.pgsqldb.org/" target=_top
rel=nofollow>PostgreSQL中文站</A> </LI>
<LI><A href="http://www.cpsol.net/" target=_top rel=nofollow>中国项目服务在线</A>
</LI></UL></DIV><!-- /patternLeftBarContents--></DIV><!-- /patternLeftBar--></DIV><!-- /patternFloatWrap-->
<DIV class=clear> </DIV></DIV><!-- /patternOuter--></DIV><!-- /patternWrapper-->
<DIV id=patternTopBar>
<DIV id=patternTopBarContents>
<TABLE style="MARGIN-TOP: 11px; WIDTH: 100%" cellSpacing=0 cellPadding=0
border=0>
<TBODY>
<TR>
<TD vAlign=center><A href="http://www.stlchina.org/"><IMG
alt="Powered by TWiki" src="C++的iostream标准库介绍.files/twikilog.jpg"
border=0></A></TD>
<TD class=patternMetaMenu vAlign=top align=right>
<UL>
<LI>
<FORM name=jumpForm
action=/twiki/bin/view.pl/Main/STLIOStreamIntro><INPUT
class="twikiInputField patternFormFieldDefaultColor" id=jumpFormField
onblur=setDefaultText(this); onfocus=clearDefaultandCSS(this); size=14
value=Jump name=topic></FORM>
<LI>
<FORM name=quickSearchForm
action=/twiki/bin/view.pl/Main/WebSearch><INPUT
class="twikiInputField patternFormFieldDefaultColor"
onblur=setDefaultText(this); onfocus=clearDefaultandCSS(this); size=14
value=Search name=search></FORM>
<LI></LI></UL></TD></TR></TBODY></TABLE></DIV></DIV><!-- /patternTopBar-->
<DIV id=patternBottomBar>
<DIV id=patternBottomBarContents><SPAN class=twikiRight><A
href="http://twiki.org/"><IMG
title="This site is powered by the TWiki collaboration platform" height=15
alt="This site is powered by the TWiki collaboration platform"
src="C++的iostream标准库介绍.files/T-logo-80x15.gif" width=80
border=0></A></SPAN>Copyright © by the contributing authors. All material on
this collaboration platform is the property of the contributing authors.
<BR>Ideas, requests, problems regarding TWiki? <A
href="mailto:winter_lb@yahoo.com.cn?subject=TWiki Feedback on Main.STLIOStreamIntro">Send
feedback</A> </DIV><!-- /patternBottomBarContents--></DIV><!-- /patternBottomBar--></DIV><!-- /patternPage--></DIV><!-- /patternPageShadow--></DIV><!-- /patternScreen--></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -