课件C程序设计zw13_第1页
课件C程序设计zw13_第2页
课件C程序设计zw13_第3页
课件C程序设计zw13_第4页
课件C程序设计zw13_第5页
已阅读5页,还剩13页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

C+程序设计(第3版)第13章输入输出流13.1C+的输入和输出13.1.1输入输出的含义13.1.2C+的I/O对C的发展类型安全和可扩展性13.1.3C+的输入输出流1. C+的流库图13.1图13.2图13.32. 与流类库有关的头文件3. 在iostream头文件中定义的流对象13.2标准输出流13.2.1cout,cerr和clog流编写程序: #include #include using namespace std;int main() float a,b,c,disc;coutabc; if (a=0)cerra is equal to zero,error!endl;/将有关出错信息插入cerr流,在屏幕输出elseif (disc=bb-4ac)0)cerrdisc=bb-4ac0endl; /将有关出错信息插入cerr流,在屏幕输出else coutx1=(-b+sqrt(disc)/(2a)endl;coutx2=(-b-sqrt(disc)/(2a)endl;return 0;运行结果: please input a,b,c:0 2 3a is equal to zero,error! please input a,b,c:5 2 3disc=bb-4ac0 please input a,b,c:12.51.5x1=-1x2=-1.51. cout流对象2. cerr流对象3. clog流对象13.2.2标准类型数据的格式输出1. 使用控制符控制输出格式编写程序:#include using namespace std;int main() int a=21cout.setf(iosshowbase);/显示基数符号(0x或0)coutdec:aendl; /默认以十进制形式输出acout.unsetf(iosdec); /终止十进制的格式设置cout.setf(ioshex); /设置以十六进制输出的状态Bcouthex:aendl; /以十六进制形式输出acout.unsetf(ioshex); /终止十六进制的格式设置cout.setf(iosoct); /设置以八进制输出的状态coutoct:aendl; /以八进制形式输出a char pt=China;/pt指向字符串Chinacout.width(10);/指定域宽为10coutptendl;/输出字符串cout.width(10);/指定域宽为10cout.fill();/指定空白处以填充coutptendl;/输出字符串double pi=22.0/7.0;/输出pi值cout.setf(iosscientific);/指定用科学记数法输出coutpi=; /输出pi=cout.width(14);/指定域宽为14coutpiendl;/输出pi值cout.unsetf(iosscientific); /终止科学记数法状态cout.setf(iosfixed);/指定用定点形式输出cout.width(12); /指定域宽为12cout.setf(iosshowpos);/正数输出+号cout.setf(iosinternal); /数符出现在左侧cout.precision(6);/保留6位小数coutpiendl; /输出pi,注意数符+的位置return 0;运行结果: dec:21(十进制形式)hex:0x15 (十六进制形式,以0x开头)oct:025(八进制形式,以0开头) China (域宽为10) China (域宽为10,空白处以填充)pi= 3.142857e+00(指数形式输出,域宽14,默认6位小数,空白处以填充)+ 3.142857 (小数形式输出,精度为6,最左侧输出数符+)说明: (1) 成员函数width(n)和控制符setw(n)只对其后的第1个输出项有效。如cout.width(6);cout203.14endl;输出结果为203.1413.2.3用流成员函数put输出字符编写程序: #include using namespace std;int main() char p=BASIC;/字符指针指向Bfor(int i=4;i=0;i-)cout.put(p+i);/从最后一个字符开始输出cout.put(n);return 0;运行结果: CISAB编写程序:#include /也可以用#include ,同时不要下一行using namespace std;int main() char p=BASIC;for(int i=4;i=0;i-)putchar(p+i);putchar(n);运行结果:与前相同。13.3标准输入流13.3.1cin流13.3.2用于字符输入的流成员函数1. 用get函数读入一个字符编写程序:#include int main() int c;coutenter a sentence:endl;while(c=cin.get()!=EOF)cout.put(c);return 0;从键盘输入一行字符,用cin.get()逐个读入字符,将读入字符赋给字符变量c。如果c的值不等于EOF,表示已成功地读入一个有效字符,然后通过put函数输出该字符。运行结果: enter a sentence:I study C+very hard.(输入一行字符)I study C+very hard.(输出该行字符)Z(程序结束)2. 用成员函数getline函数读入一行字符13.3.3istream类的其他成员函数编写程序: #include using namespace std;int main() char c;while(!cin.eof()/eof()为假表示未遇到文件结束符if(c=cin.get()!= )/检查读入的字符是否空格字符cout.put(c);return 0;运行结果: C+is very interesting.C+isveryinteresting.Z(结束)2. peek函数peek是“观察”的意思,peek函数的作用是观测下一个字符。其调用形式为c=cin.peek();1. eof函数2. peek函数3. putback函数编写程序:#include using namespace std;int main() char c20;int ch;coutplease enter a sentence:endl;cin.getline(c,15,/);coutThe first part is:cendl;ch=cin.peek();/观看当前字符coutThe next character(ASCII code) is:chendl;cin.putback(c0); /将I插入到指针所指处 cin.getline(c,15,/);coutThe second part is:cendl;return 0;运行结果: please enter a sentence: I am a boy./ am a student./The first part is:I am a boy.The next character(ASCII code) is:32(下一个字符是空格)The second part is:I am a student.图13.44. ignore函数编写程序:先看不用ignore函数的情况: #include using namespace std;int main()char ch20;cin.get(ch,20,/);coutThe first part is:chendl;cin.get(ch,20,/);coutThe second part is:chendl;return 0;运行结果: I like C+./I study C+./I am happy.The first part is:I like C+.The second part is: (字符数组ch中没有从输入流中读取有效字符)13.4对数据文件的操作与文件流13.4.1文件的概念图13.513.4.2文件流类与文件流对象13.4.3文件的打开与关闭1. 打开磁盘文件13.4.4对ASCII文件的操作编写程序:#include using namespace std;int main() int a10;ofstream outfile(f1.dat,iosout);/定义文件流对象,打开磁盘文件f1.datif(!outfile)/如果打开失败,outfile返回0值 cerropen error!endl;exit(1);coutenter 10 integer numbers:endl;for(int i=0;iai;outfileai ;/向磁盘文件f1.dat输出数据outfile.close(); /关闭磁盘文件f1.datreturn 0;运行结果: enter 10 integer numbers:1 3 5 2 4 6 10 8 7 9 编写程序: #include using namespace std;int main() int a10,max,i,order;ifstream infile(f1.dat,iosin|iosnocreate);/定义输入文件流对象,以输入方式打开磁盘文件f1.datif(!infile) cerropen error!endl;exit(1);for(i=0;iai;/从磁盘文件读入10个整数,顺序存放在a数组中coutai ;/在显示器上顺序显示10个数coutendl;max=a0;order=0;for(i=1;imax) max=ai;/将当前最大值放在max中order=i; /将当前最大值的元素序号放在order中coutmax=maxendlorder=orderendl;infile.close();return 0;运行结果: 1 3 5 2 4 6 10 8 7 9(在磁盘文件中存放的10个数)max=10 (最大值为10)order=6(最大值是数组中序号为6的元素)编写程序: #include using namespace std;/ save_to_file函数从键盘读入一行字符并将其中的字母存入磁盘文件void save_to_file() ofstream outfile(f2.dat); /定义输出文件流对象outfile,以输出方式打开磁盘文件f2.datif(!outfile) cerropen f2.dat error!=65 & ci=97 & ci=122)/如果是字母字符 outfile.put(ci);/将字母字符存入磁盘文件f2.datcoutci;/同时送显示器显示coutendl;outfile.close(); /关闭f2.dat/从磁盘文件f2.dat读入字母字符,将其中的小写字母改为大写字母,再存入f3.datvoid get_from_file() char ch;ifstream infile(f2.dat,iosin|iosnocreate); /定义输入文件流outfile,以输入方式打开磁盘文件f2.dat if(!infile) cerropen f2.dat error!endl;exit(1);ofstream outfile(f3.dat);/定义输出文件流outfile,以输出方式打开磁盘文件f3.dat if(!outfile) cerropen f3.dat error!=97 & ch=122)/判断ch是否为小写字母ch=ch-32;/将小写字母变为大写字母outfile.put(ch); /将该大写字母存入磁盘文件f3.datcoutch;/同时在显示器输出coutendl;infile.close();/关闭磁盘文件f2.datoutfile.close(); /关闭磁盘文件f2.dat int main() save_to_file(); /调用save_to_file(),从键盘读入一行字符并将其中的字母存入磁盘文件f2.datget_from_file(); /调用get_from_file(),从f2.dat读入字母字符,改为大写字母,再存入f3.dat return 0;运行结果: New Beijing, Great Olypic, 2008, China.NewBeijingGreatOlypicChina (将字母写入磁盘文件f2.dat,同时在屏幕显示)NEWBEIJINGGREATOLYPICCHINA (改为大写字母)13.4.5对二进制文件的操作编写程序: #include using namespace std; struct student char name20; int num;int age;char sex;int main()student stud3=Li,1001,18,f,Fang,1002,19,m,Wang,1004,17,f;ofstream outfile(stud.dat,iosbinary); if(!outfile) cerropen error!endl; abort();/退出程序 for(int i=0;i3;i+) outfile.write(char)&studi,sizeof(studi); outfile.close(); return 0;编写程序:#include using namespace std;struct student string name;int num;int age;char sex;int main() student stud3;int i;ifstream infile(stud.dat,iosbinary);if(!infile) cerropen error!endl;abort();for(i=0;i3;i+)infile.read(char)&studi,sizeof(studi);infile.close();for(i=0;i3;i+) coutNO.i+1endl;coutname:endl;coutnum:studi.numendl;coutage:studi.ageendl;coutsex:studi.sexendlendl;return 0;运行结果: NO.1name: Li num: 1001age: 18sex: fNO.2name: Fang num: 1001age: 19sex: mNO.3name: Wangnum: 1004age: 17sex: f1. 用成员函数read和write读写二进制文件2. 与文件指针有关的流成员函数编写程序: #include using namespace std;struct student int num;char name20;float score;int main() student stud5=1001,Li,85,1002,Fang,97.5,1004,Wang,54,1006,Tan,76.5,1010,ling,96;fstream iofile(stud.dat,iosin|iosout|iosbinary);/用fstream类定义输入输出二进制文件流对象iofileif(!iofile) cerropen error!endl;abort();for(int i=0;i5;i+)/向磁盘文件输出5个学生的数据iofile.write(char )&studi,sizeof(studi);student stud15;/用来存放从磁盘文件读入的数据for(int i=0;i5;i=i+2) iofile.seekg(isizeof(studi),iosbeg);/定位于第0,2,4学生数据开头iofile.read(char )&stud1i/2,sizeof(stud10); /先后读入3个学生的数据,存放在stud10,stud1和stud2中coutstud1i/2.num stud1i/2.name stud1i/2.scoreendl;/输出stud10,stud1和stud2各成员的值coutendl;stud2.num=1012;/修改第3个学生(序号为2)的数据strcpy(,Wu);stud2.score=60;iofile.seekp(2sizeof(stud0),iosbeg); /定位于第3个学生数据的开头iofile.write(char )&stud2,sizeof(stud2);/更新第3个学生数据iofile.seekg(0,iosbeg); /重新定位于文件开头for(int i=0;i5;i+) iofile.read(char )&studi,sizeof(studi);/读入5个学生的数据coutstudi.num studi.scoreendl;iofile.close();return 0;运行结果: 1001 Li 85(第1个学生数据)1004 Wang 54(第3个学生数据)1010 ling 96(第5个学生数据)1001 Li 85(输出修改后5个学生数据)1002 Fang 97.51012 Wu 60(已修改的第3个学生数据)1006 Tan 76.51010 ling 963. 随机访问二进制数据文件编写程序: #include using namespace std; struct student int num;char name20;float score;int main() student stud3=1001,Li,78,1002,Wang,89.5,1004,Fang,90;char c50;/用户定义的字符数组ostrstream strout(c,30); /建立输出字符串流,与数组c建立关联,缓冲区长30for(int i=0;i3;i+) /向字符数组c写3个学生的数据studi.score;stroutends; /ends是C+的I/O操作符,插入一个0coutarray c:cendl;/显示字符数组c中的字符运行结果: array c:1001Li781002Wang89.51004Fang9013.5字符串流1. 建立输出字符串流对象2. 建立输入字符串流对象3. 建立输入输出字符串流对象编写程序:#include using namespace std;int main()char c50=12 34 65 -23 -32 33 61 99 321 32;int a10,i,j,t;coutarray c:cendl;/显示字符数组中的字符

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

最新文档

评论

0/150

提交评论