为了正常的体验网站,请在浏览器设置里面开启Javascript功能!

iostrea标准库介绍

2020-03-08 27页 doc 69KB 4阅读

用户头像

is_589748

暂无简介

举报
iostrea标准库介绍   查看文章             C++的iostream标准库介绍 0 为什么需要iostream 我们从一开始就一直在利用C++的输入输出在做着各种练习,输入输出是由iostream库提供的,所以讨论此标准库是有必要的,它与C语言的 stdio库不同,它从一开始就是用多重继承与虚拟继承实现的面向对象的层次结构,作为一个c++的标准库组件提供给程序员使用。 iostream为内置类型类型对象提供了输入输出支持,同时也支持文件的输入输出,类的设计者可以通过对iostream库的扩展,来支持自定...
iostrea标准库介绍
  查看文章             C++的iostream库介绍 0 为什么需要iostream 我们从一开始就一直在利用C++的输入输出在做着各种练习,输入输出是由iostream库提供的,所以讨论此标准库是有必要的,它与C语言的 stdio库不同,它从一开始就是用多重继承与虚拟继承实现的面向对象的层次结构,作为一个c++的标准库组件提供给程序员使用。 iostream为内置类型类型对象提供了输入输出支持,同时也支持文件的输入输出,类的设计者可以通过对iostream库的扩展,来支持自定义类型的输入输出操作。 为什么说要扩展才能提供支持呢?我们来一个示例。 #include #include using namespace std; class Test { public: Test(int a=0,int b=0) { Test::a=a; Test::b=b; } int a; int b; }; int main() { Test t(100,50); printf("%???",t);//不明确的输出格式 scanf("%???",t);//不明确的输入格式 cout<>t;//同样不够明确 system("pause"); } 由于自定义类的特殊性,在上面的代码中,无论你使用c风格的输入输出,或者是c++的输入输出都不是不明确的一个示,由于c语言没有运算符重载机制,导致stdio库的不可扩充性,让我们无法让printf()和scanf()支持对自定义类对象的扩充识别,而c++是可以通过运算符重载机制扩充 iostream库的,使系统能能够识别自定义类型,从而让输入输出明确的知道他们该干什么,格式是什么。 在上例中我们之所以用printf与cout进行对比目的是为了告诉大家,C与C++处理输入输出的根本不同,我们从c远的输入输出可以很明显看出是数调用方式,而c++的则是对象模式,cout和cin是ostream类和istream类的对象。 1 iostream: istream 和 ostream C++中的iostream库主要包含下图所示的几个头文件: IOSstream 库 fstream iomainip ios iosfwd iostream istream ostream sstream streambuf strstream     我们所熟悉的输入输出操作分别是由istream(输入流)和ostream(输出流)这两个类提供的,为了允许双向的输入/输出,由istream和ostream派生出了iostream类。 类的继承关系见下图: iostream库定义了以下三个标准流对象: 1 cin,表示标准输入(standard input)的istream类对象。cin使我们可以从设备读如数据。 2 cout,表示标准输出(standard output)的ostream类对象。cout使我们可以向设备输出或者写数据。 3 cerr,表示标准错误(standard error)的osttream类对象。cerr是导出程序错误消息的地方,它只能允许向屏幕设备写数据。 输出主要由重载的左移操作符(<<)来完成,输入主要由重载的右移操作符(>>)完成: 4 >>a表示将数据放入a对象中。 5 < using namespace std; int main()  { ofstream myfile("c:\\1.txt",ios::out|ios::trunc,0); myfile<<"中国软件开发实验室"< #include using namespace std; int main()  { ofstream myfile("c:\\1.txt",ios::app,0); if(!myfile)//或者写成myfile.fail() { cout<<"文件打开失败,目标文件状态可能为只读!"; system("pause"); exit(1); } myfile<<"中国软件开发实验室"< #include using namespace std; int main()  { ofstream myfile; myfile.open("c:\\1.txt",ios::out|ios::app,0); if(!myfile)//或者写成myfile.fail() { cout<<"文件创建失败,磁盘不可写或者文件为只读!"; system("pause"); exit(1); } myfile<<"中国软件开发实验室"< #include #include using namespace std; int main()  { ifstream myfile; myfile.open("c:\\1.txt",ios::in,0); if(!myfile) { cout<<"文件读错误"; system("pause"); exit(1); } char ch; string content; while(myfile.get(ch)) { content+=ch; cout.put(ch);//cout< #include using namespace std; int main()  { fstream myfile; myfile.open("c:\\1.txt",ios::out|ios::app,0); if(!myfile) { cout<<"文件写错误,文件属性可能为只读!"< #include using namespace std; int main()  { char *name = "www.cndev-lab.com"; int arraysize = strlen(name)+1; istrstream is(name,arraysize); char temp; is>>temp; cout< #include using namespace std; int main()  { int arraysize=1; char *pbuffer=new char[arraysize]; ostrstream ostr(pbuffer,arraysize,ios::out); ostr< #include #include using namespace std; int main()  { stringstream ostr("ccc"); ostr.put('d'); ostr.put('e'); ostr<<"fg"; string gstr = ostr.str(); cout<>a; cout< #include #include using namespace std; int main()  { stringstream sstr; //--------int转string----------- int a=100; string str; sstr<>str; cout<>cname; cout<记录
信息。这些当前的状态信息被包含在io_state类型的对象中。io_state是一个枚举类型(就像open_mode一样),以下便是它包含的值。 l goodbit 无错误 l Eofbit 已到达文件尾 l failbit 非致命的输入/输出错误,可挽回 l badbit 致命的输入/输出错误,无法挽回 有两种方法可以获得输入/输出的状态信息。一种方法是通过调用rdstate()函数,它将返回当前状态的错误标记。例如,假如没有任何错误,则rdstate()会返回goodbit.下例示例,表示出了rdstate()的用法: #include using namespace std; int main()  { int a; cin>>a; cout< using namespace std; int main()  { int a; cin>>a; cout< using namespace std; int main()  { int a; cin>>a; cout< using namespace std; int main()  { int a; while(1) { cin>>a; if(!cin)//条件可改写为cin.fail() { cout<<"输入有错!请重新输入"< #include using namespace std; int main()  { ifstream myfile("c:\\1.txt",ios_base::in,0); if(myfile.fail()) { cout<<"文件读取失败或指定文件不存在!"<内容
已经全部读完"<
/
本文档为【iostrea标准库介绍】,请使用软件OFFICE或WPS软件打开。作品中的文字与图均可以修改和编辑, 图片更改请在作品中右键图片并更换,文字修改请直接点击文字进行修改,也可以新增和删除文档中的内容。
[版权声明] 本站所有资料为用户分享产生,若发现您的权利被侵害,请联系客服邮件isharekefu@iask.cn,我们尽快处理。 本作品所展示的图片、画像、字体、音乐的版权可能需版权方额外授权,请谨慎使用。 网站提供的党政主题相关内容(国旗、国徽、党徽..)目的在于配合国家政策宣传,仅限个人学习分享使用,禁止用于任何广告和商用目的。
热门搜索

历史搜索

    清空历史搜索