合工大计算机学院程序设计10第十章c的输入输出

上传人:san****019 文档编号:70059495 上传时间:2019-01-15 格式:PPT 页数:21 大小:262.31KB
返回 下载 相关 举报
合工大计算机学院程序设计10第十章c的输入输出_第1页
第1页 / 共21页
合工大计算机学院程序设计10第十章c的输入输出_第2页
第2页 / 共21页
合工大计算机学院程序设计10第十章c的输入输出_第3页
第3页 / 共21页
合工大计算机学院程序设计10第十章c的输入输出_第4页
第4页 / 共21页
合工大计算机学院程序设计10第十章c的输入输出_第5页
第5页 / 共21页
点击查看更多>>
资源描述

《合工大计算机学院程序设计10第十章c的输入输出》由会员分享,可在线阅读,更多相关《合工大计算机学院程序设计10第十章c的输入输出(21页珍藏版)》请在金锄头文库上搜索。

1、第十章 C+的输入输出,流 文件 文件流的使用:文件的输入和输出,10.1 流与文件 一、流,C+语言提供了流用于读写文件。 接收输出数据的地方称为目标,发出输入数据的地方称为源。输入输出操作可以看作是字符序列在源、目标与对象之间的流动,因而将完成输入输出操作的类称为流类。 源流 目标流 输入缓冲区(读) 输出缓冲区(写) 流的提取运算符 流的插入运算符,二、文件,一个文件必须有一个由用户命名的唯一名字,可用于存储程序和数据。 C+语言将文件看作是一个字符的序列,即由一个个字节的数据顺序组成流式文件 根据文件中数据的组织形式,文件分为: 文本文件:每一个字节存放的是一个ASCII码,表示一个字

2、符,以字符为单位存储文件。 二进制文件:可以是任何类型的文件,以二进制位bit为单位存储文件。,10.2 文件的输入和输出,读写文件的过程: 往流中插入一个字符; 从流中提取一个字符; 一旦建立一个流,把它与文件相关联 打开一个文件流,以便用来读和写。使用完后,需要关闭该文件流。,一、文件的打开和关闭 1. C+的三种文件流,输入文件流:类 ifstream 输出文件流:类 ofstream 输入/输出文件流:类 fstream 三种文件流都位于 库 fstream。 例: #include void main( ) / 建立不同的文件流 ifstream in; /input file st

3、ream只能读 ofstream out; /output file stream只能写 fstream both; / I/O file stream ,2. 文件的打开与关闭 方法一:使用函数open,一旦建立一个流,将它与文件相关联的一种方法是使用函数 open( ); ofstream ofile; / 创建一个输出文件流 ofile.open( “text.txt” ) ; / 打开文件text.txt,使ofile流与该文件相关联 / 写文件text.txt ofile.close( ); / 关闭text.txt文件 ofile.open( “other.txt” ) ; /使o

4、file流与另一文件other.txt相关联:重用流 / 写文件other.txt ofile.close( ); / 关闭other.txt文件,方法二:通过流类的构造函数在创建流时直接与某文件相关联,ofstream ofile ( “text.txt” ); / 创建一个输出文件流ofile / 打开文件text.txt,使ofile流与该文件相关联 / 写文件text.txt ofile.close( ); / 关闭text.txt文件 流类的构造函数的参数和缺省值与open函数相同,用于在创建一个流时就使它与某个文件相关联,3. open函数的原型,P308 open()的函数原型为

5、: void open(const char* filename, int mode, int prot = filebuf:openprot); filename:文件名字,可含路径 mode:文件的打开模式 prot:文件的访问方式:普通、只读、隐藏、系统文件,与os相关,一般使用缺省值,文件打开模式mode,ios类中定义了模式的枚举类型open_mode p397 ifstream类中,open()的mode参数缺省值是ios:in ofstream类中,open()的mode参数缺省值是ios:out 缺省方式下,文件以文本方式打开,mode参数用位或运算|组合枚举常量 ofstre

6、am ofile; ofile.open( “test.bak”, ios:out|ios:binary ) ; / 以二进制形式打开当前目录下的test.bak文件,准备进行写操作 打开失败:重载!运算符 if ( ! ofile ) 警告打开文件失败; 退出程序; 继续正常的文件操作;,二、文件的读写,文本文件的读操作是从流中取一个字符 流的提取运算符 文本文件的写操作是向流中写一个字符 流的插入运算符,例1:将文件file_from拷贝到文件file_to中去,#include #include int CopyFile( char *file_from, char *file_to)

7、/ file_from: 输入文件名(源文件);file_to: 输出文件名(目标文件) char ch; /文件不存在则打开失败 ifstream from(file_from,ios:in|ios:nocreate); /以文本方式打开 if (!from) cout“Cannot open “file_from“ for input!n“; return 0 ; / 返回0表示复制文件不成功 ofstream to(file_to); / 文件不存在则创建新文件 if (!to) cout“Cannot open “file_to“ for output!n“; return 0 ; /

8、从流from中读一个字符到ch中,成功的话则写入to,否则文 /件的每个字符都读完了 while (from.get(ch) to.put(ch); from.close(); to.close(); / 关闭文件 return 1; / 返回1表示文件复制成功 ,例2:把一个整数、一个浮点数和一个字符串写到名为out.txt的文本文件中,#include int main( ) ofstream outfile; outfile.open(“out.txt“); if (!outfile) cout“Cannot open out.txt for output!n“; return 0 ;

9、/ 返回0表示打开文件不成功 float f = 123.23; int n = 10; outfile n “ “ f “n“; outfile“This is a text file“; outfile.close(); return 1; ,程序执行后,在当前目录下生成文件out.Txt,文件内容如下: 10 123.230003 This is a text file,例3:从例2程序建立的文件中读入相关数据,#include #include int main( ) ifstream infile; infile.open(“out.txt“); if (!infile) cout

10、n f ch str; cout n “ “ f “ “ ch “n“; cout str ; infile.close(); return 1; ,输出结果: 10 123.230003 T his,缺省情况下,运算符号跳过空白符号(如空格,回车),然后读入对应于输入对象类型的字符,例4:百钱买百鸡,#include #include void main() int i,j,k; char a28; ofstream ofile(“d:myfile.txt“);/打开文件 ofile“ 公鸡 母鸡 小鸡“endl; for(i=0;i=20;i+) for(j=0;j=33;j+) k=10

11、0-i-j; if(5*i+3*j+k/3=100),ifstream ifile(“d:myfile.txt“); i=0; while(ifile.get(ai)/不可用,它不能读白字符 if(ai=n) break; i+; / 把文件中的第一行(到n)内容读入a字符数组 ai=0; / a加上结束符,成为完整的字符串 coutijk;/由文件读入 if(ifile.eof()!=0) break; coutsetw(6)isetw(10)jsetw(10)kendl;/屏幕显示 ifile.close(); ,myfile.txt,公鸡 母鸡 小鸡 0 25 75 4 18 78 8

12、11 81 12 4 84,例5,#include /包含 #include #include class inventory / 产品目录 char Description20; char No10; int Quantity; double Cost; double Retail; /零售价 public: inventory(char* =“#“,char* =“0“,int =0,double =0,double =0); void display(); Bdatatofile(ofstream,inventory:inventory(char *des,char *no,int qu

13、an,double cost,double ret) strcpy(Description,des); strcpy(No,no); Quantity=quan; Cost=cost; Retail=ret; void inventory:display() cout.setf(ios:left); coutsetw(20)Descriptionsetw(10)No; cout.unsetf(ios:left);/要改为右对齐,先清左对齐 cout.setf(ios:right); coutsetw(10)Quantitysetw(10)Costsetw(10)Retailendl; ,inv

14、entory:Bdatatofile(ofstream /由此可见读和写是完全对称的过程,次序决不能错,void main() inventory car1(“夏利2000“,“805637928“,156,80000,105000),car2; inventory motor1(“金城125“,“93612575“,302,10000,13000),motor2; ofstream ddatafile(“d:Ex9_10.data“,ios:out|ios:binary); car1.Bdatatofile(ddatafile); motor1.Bdatatofile(ddatafile);

15、 cout“对象car1:“endl; car1.display(); cout“对象motor1:“endl; motor1.display(); cout“对象car2:“endl; car2.display(); cout“对象motor2:“endl; motor2.display(); ddatafile.close(); ifstream sdatafile(“d:Ex9_10.data“,ios:in|ios:binary);/重新打开文件,从头读取数据 car2.Bdatafromfile(sdatafile);/从文件读取数据拷贝到对象car2 if(sdatafile.eof()=0) cout“读文件成功“endl; cout“对象car2:“endl; car2.display(); motor2.Bdatafromfile(sdatafile);/继续从文件读取数据拷贝到对象motor2 if (sdatafile.eof()=0) cout“读文件成功“endl; cout“对象motor2:“endl; motor2.display(); sdatafile.close(); ,

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

最新文档


当前位置:首页 > 高等教育 > 大学课件

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