C++高级程序设计:第14章 输入输出流(例子)

上传人:努力****83 文档编号:164050843 上传时间:2022-10-24 格式:DOC 页数:11 大小:48.50KB
收藏 版权申诉 举报 下载
C++高级程序设计:第14章 输入输出流(例子)_第1页
第1页 / 共11页
C++高级程序设计:第14章 输入输出流(例子)_第2页
第2页 / 共11页
C++高级程序设计:第14章 输入输出流(例子)_第3页
第3页 / 共11页
资源描述:

《C++高级程序设计:第14章 输入输出流(例子)》由会员分享,可在线阅读,更多相关《C++高级程序设计:第14章 输入输出流(例子)(11页珍藏版)》请在装配图网上搜索。

1、首先来看一看在ios.h中有关ios类的定义的部分内容:class _CRTIMP ios public: enum skipws = 0x0001, /公有的无名枚举成员 left = 0x0002, right = 0x0004, internal = 0x0008, dec = 0x0010, oct = 0x0020, hex = 0x0040, showbase = 0x0080, showpoint = 0x0100, uppercase = 0x0200, showpos = 0x0400, scientific = 0x0800, fixed = 0x1000, unitbuf

2、 = 0x2000, stdio = 0x4000 ; static const long basefield; / dec | oct | hex static const long adjustfield; / left | right | internal static const long floatfield; / scientific | fixed. inline long flags( ) const; /公有的成员函数 inline long flags(long _l); inline long setf(long _f, long _m); inline long set

3、f(long _l); inline long unsetf(long _l); inline int width( ) const; inline int width(int _i); inline char fill( ) const; inline char fill(char _c); inline int precision(int _i); inline int precision( ) const;protected: /保护的数据成员 long x_flags; / 输入输出状态字 int x_precision; / 输入输出精度 char x_fill; / 填充字符 in

4、t x_width; / 输出数据的域宽 .;系统根据这几个数据成员的值,来控制输入输出格式。通过上述公有成员函数来设置这几个状态字。各种状态由公有的无名枚举成员定义。返回ppt讲稿 例14.18 用友元函数实现复数类对象插入和提取运算符重载函数#includeclass Complexdouble Real, Image;public:Complex(double r=0, double i=0)Real=r; Image=i;friend istream& operator(istream &, Complex &);friend ostream& operator(istream &in

5、, Complex &c) 注意参数和返回值 inc.Realc.Image;return in;ostream& operator(ostream &out, Complex &c) 注意参数和返回值 out0) out+c.Imagei; else if(c.Image0) outc.Imagei; outendl;return out;void main( )Complex c1(1, 2), c2;coutc1; coutc1c2; /解释为operator(operator(cin, c1), c2); coutc1c2; /解释为operator(operator(cout, c1

6、), c2); 程序的运行状况如下: 1+2i Please input c1 & c2: 5 8 /输入 7 -3 /输入 5+8i 7-3i 返回ppt讲稿 例14.24 编一个程序用于复制文本文件 #include #include #include void main( )char infilename40, outfilename40, ch; coutinfilename; coutoutfilename; fstream infile(infilename, ios:in|ios:nocreate); if(!infile) coutCan not open input file

7、: infilenameendl;exit(1); fstream outfile(outfilename, ios:out); if(!outfile) coutCan not open output file: outfilenameendl;exit(2); while(infile.get(ch) outfilech; infile.close( ); outfile.close( ); 返回ppt讲稿 例14.25 编一个程序从一个文本文件source.txt中读入若干整数,用选择法将这些数据排成升序,将排序后的结果写入另一个文件文本文件target.txt中。注意两个文件均在d盘的

8、data文件夹中。#include #include #include void sort(int *a, int n) /一般的选择法排序函数int i, j, p, t;for(i=0; in-1; i+)p=i;for(j=i+1; jn; j+)if(ajap) p=j;if(p!=i) t=ai;ai=ap; ap=t; void main( )int a100, i, n;fstream in, out; /若路径缺省,指当前目录in.open(d:datasource.txt, ios:in|ios:nocreate); if(!in) coutCan not open sour

9、ce.txt!endl;exit(1); out.open(d:datatarget.txt, ios:out); if(!out) coutCan not open target.txt!ai) i+; /循环结束后,i是整数的个数sort(a, i ); n=i; for(i=0; in; i+) outaiendl; in.close( ); out.close( ); 在程序运行前,先准备好输入数据文件source.txt,放入d盘的data文件夹中,内容可以如下:2 31045338 9204567888 37 2 32-20-1程序运行结束后,查看d盘的data文件夹中的结果文件t

10、arget.txt内容是否正确。返回ppt讲稿 例 14.28 读入文本文件data.txt 中的数据, 写入二进制文件data.bin中 。#include #include #include void main( )int a100, n=0;ifstream infile(data.txt, ios:in|ios:nocreate);if( !infile ) coutCan not open input file: data.txtan+; / 读入到数组中infile.close( );ofstream outfile(data.bin, ios:out|ios:binary);if

11、( !outfile ) coutCan not open output file: data.datendl;exit(2); outfile.write(char *)&n, sizeof(int); /写出元素个数outfile.write(char *)a, n*sizeof(int); /写出整个数组outfile.close( );返回ppt讲稿 例14.27 编写一个程序对二进制文件进行读写。本程序的功能是,从键盘输入若干学生的信息,写入二进制文件,再从该二进制文件中读出学生的信息,输出到屏幕上。#include #include #include #include struct

12、 student /定义一个结构体类型char name10; /姓名char id10; /学号int score; /分数;#define LEN sizeof(struct student)void main( )student st;fstream file(stud.dat, ios:out|ios:binary); /以二进制方式打开输出文件if(!file) coutCan not open output file: stud.datst.name;while(strcmp(st.name, #)!=0) /循环输入时,以输入姓名为“#”结束cinst.idst.score; /

13、循环从键盘输入学生信息file.write(char *)&st, LEN); /一次写出LEN字节的内存数据cinst.name;file.close( ); /关闭与file关联的文件,以便后面重复使用file对象student sts100;int i=0, j;file.open(stud.dat, ios:in|ios:binary|ios:nocreate); / 重复使用file对象if(!file) coutCan not open input file: stud.datendl;exit(2); while(file.read(char *)(sts+i), LEN) /一次读入LEN字节的数据,存入内存指定地址i+;for(j=0; ji; j+) /循环向屏幕输出学生信息coutstsj.nametstsj.idtstsj.scoreendl;file.close( );返回ppt讲稿

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

copyright@ 2023-2025  zhuangpeitu.com 装配图网版权所有   联系电话:18123376007

备案号:ICP2024067431-1 川公网安备51140202000466号


本站为文档C2C交易模式,即用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知装配图网,我们立即给予删除!