第十一讲文件和流.ppt

上传人:本田雅阁 文档编号:2261978 上传时间:2019-03-13 格式:PPT 页数:22 大小:109.51KB
返回 下载 相关 举报
第十一讲文件和流.ppt_第1页
第1页 / 共22页
第十一讲文件和流.ppt_第2页
第2页 / 共22页
第十一讲文件和流.ppt_第3页
第3页 / 共22页
亲,该文档总共22页,到这儿已超出免费预览范围,如果喜欢就下载吧!
资源描述

《第十一讲文件和流.ppt》由会员分享,可在线阅读,更多相关《第十一讲文件和流.ppt(22页珍藏版)》请在三一文库上搜索。

1、第十一章 文件和流,流:用于输入输出地一组类 主要内容:文本流、二进制流,11.1 输入流和输出流 C无专门的输入输出语句,是由流库完成;流与特定的设备相联系。,11.1.1 输出流 cout : 输出流ostream类的一个预定义对象 与标准输出设备(终端屏幕)相联系 ostream中重载了运算符(插入运算符) 插入运算符: ostream /重载形式:注意优先级和结合顺序,cout“string”;,注意:C编译器将根据对象类型选用相应版本的重载)运算符函数,用户不必关心。,读入一个字符串时,空格作为串的终止。 char buffer20; cinbuffer; /输入“Jack Spar

2、t”,读入“Jack”,类型不符,返回零值,并终止程序。 int readints( ) int v10; for(int i=0; ivi) continue; return i; / ,输入:1 2 3 4 5. 6 7 8,11.1.4 重载插入和析取运算符(对用户定义类型) class Complex double rpart, ipart; public: friend ostream,#include class Complex double rpart, ipart; public: Complex(double r=0.0, double i=0.0) ipart=i; par

3、t=r; friend ostream,istream ,11.2 格式化输入/输出 用ios类中定义的格式成员函数: class ios / public: int width(int w); /设置字段宽度 int width( ) const; /返回设置字段的宽度 char fill(char); /设置填充字符 char fill( ) const; /返回设置的填充字符 long flags(long f); long flags( ) const; long setf(long setbits, long field); long setf(long); long unsetf(

4、long); int precision(int); /设置浮点数精度 int precision( ) const; /返回设置的浮点数精度 / ;,函数的使用: 输入流: char buffer20; cin.width(20); cinbuffer;,输出流: cout.width(4); cout(12) ; cout.width(4); cout.fill(#); cout(12) ;,( 12),( #12),Width( )作用于输入/输出的数字或串,数据的长度超过width,忽略设置,按数据实际长度显示; cout.width(4); cout(121212) ; 每次插入操作

5、后,width被置0; cout.width(4); cout.fill(#); cout(12“),(“ 12) ;,11.2.2 格式状态 Ios中用枚举记录标志,控制I/O class ios public: enum skipws=01, /析取操作忽略空白字符 left=02, right=04, internal=010, /值按右对齐,符号按左对齐 dec=020, oct=040, hex=0100, showbase=0200, showpoint=0400, /float,double显示小数和尾数后的零 uppercase=01000, showpos=02000, /在

6、正整数前插入“+”号 scientific=04000, /科学计数法,小数点前一位数字 / ; / ;,可用下列函数设置、读取、取消标志位; long flags( ) /返回当前格式化标志值 long flags(long f) /设置标志值f,并返回上次标志值 long setf(long f) /设置标志位f,并返回上次标志位 long unsetf(long f) /取消在f中设置的标志位,并返回上次标志位,举例: void your_function( ) long old_options=cout.flags(ios:left | ios:oct | ios:showpoint)

7、; / cout.flags(old_options); ,相抵触的标志不能同时设置,如:ios:dec和ios:oct 带伪参数的setf( )指明设置哪类选项,自动清除与新设置矛盾的旧选项。 cout1234 endl; cout.setf(ios:oct, ios:basefield); cout1234 endl; cout1234 endl; cout.setf(ios:hex, ios:basefield); cout1234 endl;,伪参数:基数设置位,作用范围:下一标志位设置,cout.flags(cout.flags( ) | ios:showbase);,结果: 123

8、4 2322 2322 4d2,11.3 控制符 函数控制不方便,宽度控制符,预定义控制符有: hex dec oct指定基数,缺省dec ws用于输入流,略去空白 endl换行 ends插入一个NULL(0)字符,结束一个字符串 flush强制将流从缓冲区写入相应设备 setfill(char f)设置填充字符,缺省委空格 setprecision(int p)设浮点数精度,缺省为6 ,int x=1, y=2; coutsetw(5)xsetw(4)y;,11.5 文件和流 处理文件的类在fstream.h中定义。 文件输入输出:1、创建流对象 2、使用流的成员函数打开文件,输出流对象my

9、_file与文件hello.dat相联系,#include #lnclude void main( ) ofstream my_file; my_file.open(“hello.dat”, ios:out); / ,11.5.1 打开文件 输入打开文件用ifstream类;输出打开文件用ofstream类; 输入 输出打开文件用fstream类。,打开文件: void open(char* name, int mode, int file_attrb); mode为下列一些方式: ios:app附加方式写到流 ios:ate打开文件,并把文件指针移到文件尾 ios:in为读打开 ios:ou

10、t为写打开 ios:trunc如文件存在,舍去文件内容 ios:nocreate文件不存在,则失败 ios:noreplace文件存在,则失败 file_attrb文件属性: 普通文件、只读文件、隐藏文件。,#include #lnclude void main( ) ofstream my_file; my_file.open(“hello.dat”); my_file“Hello world”endl; my_file.close( ); ,11.5.2 按正文方式读文件 #lnclude void main( ) char string120, string220; ifstream m

11、y_file(“hello.dat”); my_filestring1; my_filestring2; coutstring1 string2endl; my_file.close( ); ,11.5.3 按二进制方式读/写文件 读写的数据无含义,不用,而是get( )和put( ) istream ,读数据: #lnclude void main( ) char my_char; int i=0; ifstream my_in_file(“hello.dat”); while(my_in_file) my_in_file.get(my_char ); coutmy_char; my_in_

12、file.close( ); ,11.5.4 使用read( ) 和write( )函数 一次读写多个字符,其原型: istream ,11.5.5 使用文件指针 用istream中成员函数seekg( )定位读指针; 用ostream中成员函数seekp( )定位写指针。 其原型: istream seek_dir三种取值: ios:beg ios:end ios:cur,读写指针在文件中位置,指出指针相对于何处,#lnclude #include const int INDEX=15; class Entry public: char name20; float owes; my_dataINDEX, my_record; void main( ) ifstream my_file1(“file.dat”); my_file1.seekg(9*sizeof(Entry), ios:beg); my_file1.read(char*) ,

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 其他


经营许可证编号:宁ICP备18001539号-1