第十一讲文件和流

上传人:鲁** 文档编号:571289476 上传时间:2024-08-09 格式:PPT 页数:22 大小:109.52KB
返回 下载 相关 举报
第十一讲文件和流_第1页
第1页 / 共22页
第十一讲文件和流_第2页
第2页 / 共22页
第十一讲文件和流_第3页
第3页 / 共22页
第十一讲文件和流_第4页
第4页 / 共22页
第十一讲文件和流_第5页
第5页 / 共22页
点击查看更多>>
资源描述

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

1、第十一章 文件和流流:用于输入输出地一组类主要内容:文本流、二进制流11.1 输入流和输出流输入流和输出流C无专门的输入输出语句,是由流库完成;流与特定的设备相联系。iostream.h流类地继承体系流类地继承体系iosostreamistreamclogfstreamiostreamcerrcinifstreamofstreamcout11.1.1 输出流输出流 cout : 输出流ostream类的一个预定义对象 与标准输出设备(终端屏幕)相联系 ostream中重载了运算符(插入运算符)插入运算符: ostream& operator(类型); /重载形式:注意优先级和结合顺序程序程序输

2、入设备输入设备输出设备输出设备文件文件 cout“string”; cout.operatori; cin.operator(i); 析取运算符:跳过开始空白字符。 char ch; cinch; /输入“ x”,读入x 注意:注意:C编译器将根据对象编译器将根据对象类型选用相应版本的重载类型选用相应版本的重载)运算符函数,用户不必关心运算符函数,用户不必关心。 读入一个字符串时,空格作为串的终止。 char buffer20; cinbuffer; /输入“Jack Spart”,读入“Jack”类型不符,返回零值,并终止程序。类型不符,返回零值,并终止程序。 int readints( )

3、 int v10; for(int i=0; ivi) continue; return i; / 输入:输入:1 2 3 4 5. 6 7 811.1.4 重载插入和析取运算符(对用户定义类型)重载插入和析取运算符(对用户定义类型) class Complex double rpart, ipart; public: friend ostream&operator(ostream &s, Complex &c); / ;第一个参数是流第一个参数是流第二个参数是要第二个参数是要输入输出的对象输入输出的对象返回流的引用返回流的引用 #include class Complex double rp

4、art, ipart; public: Complex(double r=0.0, double i=0.0) ipart=i; part=r; friend ostream&operator(istream &, Complex &); friend ostream&operator(istream& s, Complex &c) sc.rpartc.ipart; return s; ostream& operator(ostream& s, Complex &c) s(c.rpart,c.ipartc; cout“Complex:”cbuffer;输出流: cout.width(4); c

5、out(12) ; cout.width(4); cout.fill(#); cout(12) ;( 12)( #12)Width( )作用于输入作用于输入/输出的数字或串输出的数字或串 数据的长度超过width,忽略设置,按数据实际长度显示; cout.width(4); cout(121212) ;每次插入操作后,width被置0; cout.width(4); cout.fill(#); cout(12“),(“ 12) ;(121212)( #12) , (12)11.2.2 格式状态格式状态Ios中用枚举记录标志,控制I/O class ios public: enum skipws

6、=01, /析取操作忽略空白字符析取操作忽略空白字符 left=02, right=04, internal=010, /值按右对齐,符号按左对齐值按右对齐,符号按左对齐 dec=020, oct=040, hex=0100, showbase=0200, showpoint=0400, /float,double显示小数和尾数后的零显示小数和尾数后的零 uppercase=01000, showpos=02000, /在正整数前插入在正整数前插入“+”号号 scientific=04000, /科学计数法,小数点前一位数字科学计数法,小数点前一位数字 / ; / ;可用下列函数设置、读取、取

7、消标志位; 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); / cout.fla

8、gs(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; 原选项基础原选项基础上增加上增加showbase伪参数:基数设置位伪参数:基数设置位作用范围:作用范围:下一标志位设置下一标志位设置 cout.flags(cout.fla

9、gs( ) | ios:showbase); cout.setf(ios:showbase); 结果结果: 1234 2322 2322 4d2 11.3 控制符控制符函数控制不方便int x=1, y=2cout.width(5);coutx;cout.width(4);couty;宽度控制符宽度控制符预定义控制符有: hex dec oct指定基数,缺省dec ws用于输入流,略去空白 endl换行 ends插入一个NULL(0)字符,结束一个字符串 flush强制将流从缓冲区写入相应设备 setfill(char f)设置填充字符,缺省委空格 setprecision(int p)设浮点

10、数精度,缺省为6 . int x=1, y=2;coutsetw(5)xsetw(4)y;控制符如控制符如endl11.5 文件和流文件和流处理文件的类在fstream.h中定义。文件输入输出:1、创建流对象 2、使用流的成员函数打开文件文件与流建立起联系文件与流建立起联系输出流对象输出流对象my_file与与文件文件hello.dat相联系相联系 #include #lnclude void main( ) ofstream my_file; my_file.open(“hello.dat”, ios:out); / 11.5.1 打开文件打开文件输入打开文件用ifstream类;输出打开文

11、件用ofstream类;输入 输出打开文件用fstream类。打开文件: void open(char* name, int mode, int file_attrb);mode为下列一些方式: ios:app附加方式写到流 ios:ate打开文件,并把文件指针移到文件尾 ios:in为读打开 ios:out为写打开 ios:trunc如文件存在,舍去文件内容 ios:nocreate文件不存在,则失败 ios:noreplace文件存在,则失败file_attrb文件属性: 普通文件、只读文件、隐藏文件。 #include #lnclude void main( ) ofstream my_

12、file; my_file.open(“hello.dat”); my_file“Hello world”endl; my_file.close( ); ofstream my_file(“hello.dat”);11.5.2 按正文方式读文件按正文方式读文件 #lnclude void main( ) char string120, string220; ifstream my_file(“hello.dat”); my_filestring1; my_filestring2; coutstring1 string2endl; my_file.close( ); 11.5.3 按二进制方式读

13、按二进制方式读/写文件写文件 读写的数据无含义,不用,而是get( )和put( ) istream & get(char &c); ostream& put(char &c);写数据: #lnclude void main( ) char my_char; static char string =“Hello world”; int i=0; ofstream my_out_file(“hello.dat”); while(stringi) my_out_file.put (stringi+ ); my_out_file.close( ); 读数据: #lnclude void main(

14、) 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_file.close( ); 11.5.4 使用使用read( ) 和和write( )函数函数一次读写多个字符,其原型: istream & read(unsigned char* char_buffer, int number_bytes); ostream& write(const unsigned char* char_buffer , int

15、number_bytes); #lnclude #include void main( ) static char hello_buf =“hellow world.n”; ofstream my_out_file(“hello.dat”); my_out_file.write(hello_buf, sizeof(hello_buff); ifstream my_in_file(“hello.dat”); my_in_file.read(hello_buf, sizeof(hello_buf); my_in_file.close( ); my_out_file.close( ); 11.5.5

16、 使用文件指针使用文件指针用istream中成员函数seekg( )定位读指针;用ostream中成员函数seekp( )定位写指针。其原型: istream& seekg( streamoff file_offset, seek_dir org); ostream& seekp( streamoff file_offset, seek_dir org);seek_dir三种取值: ios:beg ios:end ios:cur 读写指针在文件中位置读写指针在文件中位置指出指针相对于何处指出指针相对于何处 #lnclude #include const int INDEX=15; class

17、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*) &my_record, sizeof(Entry); my_file1.close( ); ofstream my_file2(“file.dat”); my_record.owes=442.96; my_file2.seekp(9*sizeof(Entry), ios:beg); my_file2.write(char*)& my_record, sizeof(Entry); my_file2.close( );

展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 建筑/环境 > 施工组织

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号