第11类库与输入输出_第1页
第11类库与输入输出_第2页
第11类库与输入输出_第3页
第11类库与输入输出_第4页
第11类库与输入输出_第5页
免费预览已结束,剩余133页可下载查看

下载本文档

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

文档简介

4文件处理

各种计算机应用系统通常把一些相关信息组织起来保存在外存储器中,称为文件,并用一个名字(称为文件名)加以标识

C++把文件看成无结构的字节流, 编码方式:文本文件二进制文件 存取方式:顺序文件随机文件

ifstream、ofstream和fstream类用于内存与文件之间的数据传输4.1文件和流012345n-1文件字节序号文件结束符文件长度=n10.5.1文件和流4.1文件和流012345n-1文件指针10.5.1文件和流4.1文件和流012345n-1文件结束10.5.1文件和流4.1文件和流012345n-1------------------------内存------------------------ifstreamfinfstreamfinoutofstreamfout读文件写文件读/写文件10.5.1文件和流4.2打开和关闭文件

文件操作的基本步骤: 打开文件 读/写文件 关闭文件1.打开文件包括建立文件流对象;与外部文件关联;指定文件的打开方式打开文件有两种方法:首先建立流对象,然后调用fstream::open()函数连接外部文件

流类对象名

;

对象名.open(文件名

,方式

);

调用流类带参数的构造函数,建立流对象的同时连接外部文件

流类对象名

(文件名

,方式

);ifstream、ofstream或fstream10.5.2打开和关闭文件1.打开文件filebuf、ifstream、ofstream、fstream的构造函数具有相同的参数和缺省值文件流的构造函数和open()函数用于打开文件,析构函数在流对象被删除之前关闭文件

open函数原型voidopen(constchar*,intmode,int=filebuf::openprot);10.5.2打开和关闭文件1.打开文件filebuf、ifstream、ofstream、fstream的构造函数具有相同的参数和缺省值文件流的构造函数和open()函数用于打开文件,析构函数在流对象被删除之前关闭文件

open函数原型voidopen(constchar*,intmode,int=filebuf::openprot);其中,第一个参数表示相关联的文件名10.5.2打开和关闭文件1.打开文件filebuf、ifstream、ofstream、fstream的构造函数具有相同的参数和缺省值文件流的构造函数和open()函数用于打开文件,析构函数在流对象被删除之前关闭文件

open函数原型voidopen(constchar*,intmode,int=filebuf::openprot);其中,第一个参数表示相关联的文件名第二个参数表示文件的打开方式10.5.2打开和关闭文件1.打开文件filebuf、ifstream、ofstream、fstream的构造函数具有相同的参数和缺省值文件流的构造函数和open()函数用于打开文件,析构函数在流对象被删除之前关闭文件

open函数原型voidopen(constchar*,intmode,int=filebuf::openprot);其中,第一个参数表示相关联的文件名第二个参数表示文件的打开方式标识常量值意义ios::in0x0001读方式打开文件ios::out0x0002写方式打开文件ios::ate0x0004打开文件时,指针指向文件尾ios::app0x0008追加方式ios::trunc0x0010删除文件现有内容ios::nocreate0x0020如果文件不存在,则打开操作失败ios::noreplace0x0040如果文件存在,则打开操作失败ios::binary0x0080二进制方式打开,默认为文本方式mode

参数文件打开方式10.5.2打开和关闭文件1.打开文件filebuf、ifstream、ofstream、fstream的构造函数具有相同的参数和缺省值文件流的构造函数和open()函数用于打开文件,析构函数在流对象被删除之前关闭文件

open函数原型voidopen(constchar*,intmode,int=filebuf::openprot);其中,第一个参数表示相关联的文件名第二个参数表示文件的打开方式第三个参数是文件的保护方式,一般只用缺省值filebuf::openprot 适应MS-DOS模式pat 适应MS-DOS模式

filebuf::sh_none

无模式

filebuf::sh_read 读模式

filebuf::sh_write 写模式文件保护模式参数10.5.2打开和关闭文件1.打开文件用第一种方式打开文件:打开一个已有文件datafile.dat,准备读:

ifstreaminfile;

//建立输入文件流对象

infile.open("datafile.dat",ios::in);

//连接文件,指定打开方式打开(创建)一个文件newfile.dat,准备写:

ofstreamoutfile;

//建立输出文件流对象

outfile.open("d:\\newfile.dat",ios::out);

//连接文件,指定打开方式

10.5.2打开和关闭文件1.打开文件第二种方法调用文件流类带参数构造函数:例1

ifstreaminfile("datafile.dat",ios::in); ofstreamoutfile("d:\\newfile.dat",ios::out); fstreamrwfile("myfile.dat",ios::in|ios::out);用或运算符“|”

连接两个表示打开方式的标识常量10.5.2打开和关闭文件1.打开文件第二种方法调用文件流类带参数构造函数:例1 ifstreaminfile("datafile.dat",ios::in); ofstreamoutfile("d:\\newfile.dat",ios::out); fstreamrwfile("myfile.dat",ios::in|ios::out);例2打开一个二进制文件进行追加操作:

ofstreamofile("d:\\binary",ios::binary|ios::app);

//对文件写操作

//…… ofile.close();10.5.2打开和关闭文件1.打开文件打开一个输入文件流 必须说明类型为ifstream的对象打开一个输出文件流 必须说明类型为ofstream的对象要建立输入和输出的文件流 必须说明类型为fstream的对象例如:

ifstreamin; //input ofstreamout; //output fstreamboth; //inputandoutput10.5.2打开和关闭文件2.关闭文件例如:

ofstreamofile; //创建输出文件流

ofile.open("myfile1"); //ofile流与文件“myfile1”相关联

…… //访问文件“myfile1”ofile.close(); //关闭文件“myfile1”ofile.open("myfile2") ; //重用ofile流

关闭文件操作包括把缓冲区数据完整地写入文件,添加文件结束标志,切断流对象和外部文件的连接若流对象的生存期没有结束,可以重用当一个流对象的生存期结束,系统也会自动关闭文件10.5.2打开和关闭文件2.关闭文件例如:

ofstreamofile;

//创建输出文件流

ofile.open("myfile1"); //ofile流与文件“myfile1”相关联

…… //访问文件“myfile1”ofile.close(); //关闭文件“myfile1”ofile.open("myfile2") ; //重用ofile流

关闭文件操作包括把缓冲区数据完整地写入文件,添加文件结束标志,切断流对象和外部文件的连接若流对象的生存期没有结束,可以重用当一个流对象的生存期结束,系统也会自动关闭文件等价于使用构造函数:ofstreamofile("myfile1");10.5.2打开和关闭文件2.关闭文件例如:

ofstreamofile; //创建输出文件流

ofile.open("myfile1"); //ofile流与文件“myfile1”相关联

…… //访问文件“myfile1”

ofile.close();

//关闭文件“myfile1”ofile.open("myfile2") ; //重用ofile流

关闭文件操作包括把缓冲区数据完整地写入文件,添加文件结束标志,切断流对象和外部文件的连接若流对象的生存期没有结束,可以重用当一个流对象的生存期结束,系统也会自动关闭文件clos()函数关闭文件但流对象仍然存在10.5.2打开和关闭文件2.关闭文件例如:

ofstreamofile; //创建输出文件流

ofile.open("myfile1"); //ofile流与文件“myfile1”相关联

…… //访问文件“myfile1”ofile.close(); //关闭文件“myfile1”ofile.open("myfile2");

//重用ofile流

关闭文件操作包括把缓冲区数据完整地写入文件,添加文件结束标志,切断流对象和外部文件的连接若流对象的生存期没有结束,可以重用当一个流对象的生存期结束,系统也会自动关闭文件重用流对象10.5.2打开和关闭文件#include<fstream.h>voidmain(){ofstreamost; ost.open("d:\\my1.dat"); ost<<20<<endl; ost<<30.5<<endl;ost.close(); ifstreamist("d:\\my1.dat"); intn;doubled;ist>>n>>d; cout<<n<<endl<<d<<endl;}例11-12open()和close()函数应用#include<fstream.h>voidmain(){ofstreamost;

ost.open("d:\\my1.dat"); ost<<20<<endl; ost<<30.5<<endl;ost.close(); ifstreamist("d:\\my1.dat"); intn;doubled;ist>>n>>d; cout<<n<<endl<<d<<endl;}//创建输出流对象10.5.2打开和关闭文件例11-12open()和close()函数应用#include<fstream.h>voidmain(){ofstreamost;

ost.open("d:\\my1.dat");

ost<<20<<endl; ost<<30.5<<endl;ost.close(); ifstreamist("d:\\my1.dat"); intn;doubled;ist>>n>>d; cout<<n<<endl<<d<<endl;}//创建输出流对象//建立文件关联,缺省为文本模式10.5.2打开和关闭文件例11-12open()和close()函数应用#include<fstream.h>voidmain(){ofstreamost; ost.open("d:\\my1.dat");

ost<<20<<endl;

ost<<30.5<<endl;ost.close(); ifstreamist("d:\\my1.dat"); intn;doubled;ist>>n>>d; cout<<n<<endl<<d<<endl;}//创建输出流对象//建立文件关联,缺省为文本模式//向流插入数据10.5.2打开和关闭文件例11-12open()和close()函数应用#include<fstream.h>voidmain(){ofstreamost; ost.open("d:\\my1.dat"); ost<<20<<endl; ost<<30.5<<endl;

ost.close();

ifstreamist("d:\\my1.dat"); intn;doubled;ist>>n>>d; cout<<n<<endl<<d<<endl;}//创建输出流对象//建立文件关联,缺省为文本模式//向流插入数据//关闭文件10.5.2打开和关闭文件例11-12open()和close()函数应用#include<fstream.h>voidmain(){ofstreamost; ost.open("d:\\my1.dat"); ost<<20<<endl; ost<<30.5<<endl;ost.close();

ifstreamist("d:\\my1.dat");

intn;doubled;ist>>n>>d; cout<<n<<endl<<d<<endl;}//创建输出流对象//建立文件关联,缺省为文本模式//向流插入数据//关闭文件//创建输入流对象并建立关联10.5.2打开和关闭文件例11-12open()和close()函数应用#include<fstream.h>voidmain(){ofstreamost; ost.open("d:\\my1.dat"); ost<<20<<endl; ost<<30.5<<endl;ost.close(); ifstreamist("d:\\my1.dat"); intn;doubled;

ist>>n>>d;

cout<<n<<endl<<d<<endl;}//创建输出流对象//建立文件关联,缺省为文本模式//向流插入数据//关闭文件//创建输入流对象并建立关联//从流提取数据10.5.2打开和关闭文件例11-12open()和close()函数应用#include<fstream.h>voidmain(){ofstreamost; ost.open("d:\\my1.dat"); ost<<20<<endl; ost<<30.5<<endl;ost.close(); ifstreamist("d:\\my1.dat"); intn;doubled;ist>>n>>d;

cout<<n<<endl<<d<<endl;}//创建输出流对象//建立文件关联,缺省为文本模式//向流插入数据//关闭文件//创建输入流对象并建立关联//从流提取数据//向预定义流插入数据10.5.2打开和关闭文件例11-12open()和close()函数应用#include<fstream.h>voidmain(){ofstreamost; ost.open("d:\\my1.dat"); ost<<20<<endl; ost<<30.5<<endl;ost.close(); ifstreamist("d:\\my1.dat"); intn;doubled;ist>>n>>d; cout<<n<<endl<<d<<endl;}//创建输出流对象//建立文件关联,缺省为文本模式//向流插入数据//关闭文件//创建输入流对象并建立关联//从流提取数据//向预定义流插入数据10.5.2打开和关闭文件例11-12open()和close()函数应用4.3文本文件

文本文件用默认方式打开文本文件用文本文件流进行读/写操作文本文件是顺序存取文件描述一个对象的信息称为一个记录文本文件本身没有记录逻辑结构通常一个逻辑记录用换行符分隔;数据项之间可以用空白符、换行符、制表符等分隔10.5.3文本文件4.3文本文件012345……ifstreamfin读文件24378inta,b;fin>>a>>b;10.5.3文本文件012345……ifstreamfin读文件24378inta,b;fin>>a>>b;a

0X00f310.5.3文本文件4.3文本文件012345……ifstreamfin读文件24378

int

a,b;fin>>a>>b

;a0X00f3b

0X004e10.5.3文本文件4.3文本文件012345……24378inta,b;fin>>a>>b;a0X00f3b0X004eintc;c=a+b;fout<<"c=

"<<c<<endl;ofstreamfout10.5.3文本文件4.3文本文件012345……inta,b;fin>>a>>b;a0X00f3b0X004eintc;c=a+b;fout<<"c=

"<<c<<endl;ofstreamfoutc

0X01412437810.5.3文本文件4.3文本文件012345……inta,b;fin>>a>>b;a0X00f3b0X004eintc;c=a+b;fout<<"c=

"<<c<<endl;ofstreamfoutc0X014124378写文件10.5.3文本文件4.3文本文件012345……inta,b;fin>>a>>b;a0X00f3b0X004eintc;c=a+b;fout<<"c=

"<<c<<endl;ofstreamfoutc0X014124378写文件c="c=

"10.5.3文本文件4.3文本文件012345……inta,b;fin>>a>>b;a0X00f3b0X004eintc;c=a+b;fout<<"c=

"<<c<<endl;ofstreamfoutc

0X014124378写文件c=32110.5.3文本文件4.3文本文件012345……inta,b;fin>>a>>b;a0X00f3b0X004eintc;c=a+b;fout<<"c=

"<<c<<endl;ofstreamfoutc0X014124378写文件c=321endl\n10.5.3文本文件4.3文本文件012345……inta,b;fin>>a>>b;a0X00f3b0X004eintc;c=a+b;fout<<"c=

"<<c<<endl;ofstreamfoutc0X014124378c=321\n10.5.3文本文件4.3文本文件#include<fstream.h>#include<iomanip.h>voidmain(){ofstreamost;ost.open("d:\\my2.dat");ost<<"1234567890"<<endl;inta=123;ost<<a<<endl;ost<<setw(10)<<a<<endl;ost<<resetiosflags(ios::right)<<setiosflags(ios::left)<<setfill('#')<<setw(10)<<a<<endl;ost<<resetiosflags(ios::left)<<setiosflags(ios::right)<<setprecision(5)<<setw(10)<<12.34567890<<endl;ost.close();}例11-13使用格式控制建立的文本文件10.5.3文本文件#include<fstream.h>#include<iomanip.h>voidmain(){ofstreamost;ost.open("d:\\my2.dat");ost<<"1234567890"<<endl;inta=123;ost<<a<<endl;ost<<setw(10)<<a<<endl;ost<<resetiosflags(ios::right)<<setiosflags(ios::left)<<setfill('#')<<setw(10)<<a<<endl;ost<<resetiosflags(ios::left)<<setiosflags(ios::right)<<setprecision(5)<<setw(10)<<12.34567890<<endl;ost.close();}建立输出文件流对象10.5.3文本文件例11-13使用格式控制建立的文本文件#include<fstream.h>#include<iomanip.h>voidmain(){ofstreamost;

ost.open("d:\\my2.dat");ost<<"1234567890"<<endl;inta=123;ost<<a<<endl;ost<<setw(10)<<a<<endl;ost<<resetiosflags(ios::right)<<setiosflags(ios::left)<<setfill('#')<<setw(10)<<a<<endl;ost<<resetiosflags(ios::left)<<setiosflags(ios::right)<<setprecision(5)<<setw(10)<<12.34567890<<endl;ost.close();}默认方式打开文本文件10.5.3文本文件例11-13使用格式控制建立的文本文件#include<fstream.h>#include<iomanip.h>voidmain(){ofstreamost;ost.open("d:\\my2.dat");

ost<<"1234567890"<<endl;inta=123;ost<<a<<endl;ost<<setw(10)<<a<<endl;ost<<resetiosflags(ios::right)<<setiosflags(ios::left)<<setfill('#')<<setw(10)<<a<<endl;ost<<resetiosflags(ios::left)<<setiosflags(ios::right)<<setprecision(5)<<setw(10)<<12.34567890<<endl;ost.close();}插入字符串10.5.3文本文件例11-13使用格式控制建立的文本文件#include<fstream.h>#include<iomanip.h>voidmain(){ofstreamost;ost.open("d:\\my2.dat");ost<<"1234567890"<<endl;

inta=123;ost<<a<<endl;ost<<setw(10)<<a<<endl;ost<<resetiosflags(ios::right)<<setiosflags(ios::left)<<setfill('#')<<setw(10)<<a<<endl;ost<<resetiosflags(ios::left)<<setiosflags(ios::right)<<setprecision(5)<<setw(10)<<12.34567890<<endl;ost.close();}把整型数转换成字符串10.5.3文本文件例11-13使用格式控制建立的文本文件#include<fstream.h>#include<iomanip.h>voidmain(){ofstreamost;ost.open("d:\\my2.dat");ost<<"1234567890"<<endl;inta=123;ost<<a<<endl;

ost<<setw(10)<<a<<endl;ost<<resetiosflags(ios::right)<<setiosflags(ios::left)<<setfill('#')<<setw(10)<<a<<endl;ost<<resetiosflags(ios::left)<<setiosflags(ios::right)<<setprecision(5)<<setw(10)<<12.34567890<<endl

;ost.close();}以指定格式插入数据10.5.3文本文件例11-13使用格式控制建立的文本文件#include<fstream.h>#include<iomanip.h>voidmain(){ofstreamost;ost.open("d:\\my2.dat");ost<<"1234567890"<<endl;inta=123;ost<<a<<endl;

ost<<setw(10)<<a<<endl;ost<<resetiosflags(ios::right)<<setiosflags(ios::left)<<setfill('#')<<setw(10)<<a<<endl;ost<<resetiosflags(ios::left)<<setiosflags(ios::right)<<setprecision(5)<<setw(10)<<12.34567890<<endl

;ost.close();}10.5.3文本文件例11-13使用格式控制建立的文本文件#include<iostream.h>#include<fstream.h>voidmain(){ofstreamout("d:test");if(!out){cout<<"cannotopenfile.";return;}out<<10<<""<<123.45<<"";out<<"\nThisisashorttextfile.";out.close();}例11-14把一个整数、一个浮点数和一个串写到文件d:test中对数值常量做转换10.5.3文本文件#include<iostream.h>#include<fstream.h>voidmain(){charch;inti;floatf;charstr1[10],str2[10];ifstreamin("d:test");if(!in){cout<<"cannotopenfile.";return;}in>>i>>f>>ch>>str1>>str2;cout<<i<<ends<<f<<ends<<ch<<'\n';cout<<str1<<str2<<endl;in.close();}ifstreamininti

floatf

charchcharstr1[]charstr2[]10.5.3文本文件例11-15从文件d:test中读出一个整数、一个浮点数、一个字符和两个串#include<iostream.h>#include<fstream.h>voidmain(){charch;inti;floatf;charstr1[10],str2[10];ifstreamin("d:test");if(!in){cout<<"cannotopenfile.";return;}

in>>

i

>>f>>ch>>str1>>str2;cout<<i<<ends<<f<<ends<<ch<<'\n';cout<<str1<<str2<<endl;in.close();}ifstreaminintifloatf

charchcharstr1[]charstr2[]1010.5.3文本文件例11-15从文件d:test中读出一个整数、一个浮点数、一个字符和两个串#include<iostream.h>#include<fstream.h>voidmain(){charch;inti;floatf;charstr1[10],str2[10];ifstreamin("d:test");if(!in){cout<<"cannotopenfile.";return;}

in>>i>>f>>ch>>str1>>str2;cout<<i<<ends<<f<<ends<<ch<<'\n';cout<<str1<<str2<<endl;in.close();}ifstreaminfloatf

charchcharstr1[]charstr2[]123.45inti1010.5.3文本文件例11-15从文件d:test中读出一个整数、一个浮点数、一个字符和两个串#include<iostream.h>#include<fstream.h>voidmain(){charch;inti;floatf;charstr1[10],str2[10];ifstreamin("d:test");if(!in){cout<<"cannotopenfile.";return;}

in>>i>>f>>ch>>str1>>str2;cout<<i<<ends<<f<<ends<<ch<<'\n';cout<<str1<<str2<<endl;in.close();}ifstreamincharchcharstr1[]charstr2[]Tinti10floatf 123.4510.5.3文本文件例11-15从文件d:test中读出一个整数、一个浮点数、一个字符和两个串#include<iostream.h>#include<fstream.h>voidmain(){charch;inti;floatf;charstr1[10],str2[10];ifstreamin("d:test");if(!in){cout<<"cannotopenfile.";return;}

in>>i>>f>>ch>>str1>>str2;cout<<i<<ends<<f<<ends<<ch<<'\n';cout<<str1<<str2<<endl;in.close();}ifstreamincharstr1[]charstr2[]hisinti10floatf 123.45charchT10.5.3文本文件例11-15从文件d:test中读出一个整数、一个浮点数、一个字符和两个串#include<iostream.h>#include<fstream.h>voidmain(){charch;inti;floatf;charstr1[10],str2[10];ifstreamin("d:test");if(!in){cout<<"cannotopenfile.";return;}

in>>i>>f>>ch>>str1>>

str2;cout<<i<<ends<<f<<ends<<ch<<'\n';cout<<str1<<str2<<endl;in.close();}ifstreamincharstr2[]isinti10floatf 123.45charchTcharstr1[]his10.5.3文本文件例11-15从文件d:test中读出一个整数、一个浮点数、一个字符和两个串#include<iostream.h>#include<fstream.h>voidmain(){charch;inti;floatf;charstr1[10],str2[10];ifstreamin("d:test");if(!in){cout<<"cannotopenfile.";return;}in>>i>>f>>ch>>str1>>str2;

cout<<i<<ends<<f<<ends<<ch<<'\n';

cout<<str1<<str2<<endl;in.close();}inti10floatf 123.45charchTcharstr1[]hischarstr2[]isostreamcout10.5.3文本文件例11-15从文件d:test中读出一个整数、一个浮点数、一个字符和两个串#include<iostream.h>#include<fstream.h>voidmain(){charch;ifstreamf1("d:test");if(!f1){cout<<"cannotopen'test'forinput.";return;}ofstreamf2("d:testnew");if(!f2){cout<<"cannotopentestnewforouput.";return;}while(f1&&f1.get(ch))f2.put(ch);f1.close();f2.close();cout<<"Itisover!\n";}例11-16把文件d:test复制到文件d:testnew中10.5.3文本文件#include<iostream.h>#include<fstream.h>voidmain(){charch;ifstreamf1("d:test");if(!f1){cout<<"cannotopen'test'forinput.";return;}ofstreamf2("d:testnew");if(!f2){cout<<"cannotopentestnewforouput.";return;}while(f1&&f1.get(ch))f2.put(ch);f1.close();f2.close();cout<<"Itisover!\n";}ch10.5.3文本文件例11-16把文件d:test复制到文件d:testnew中例11-16把文件d:test复制到文件d:testnew中#include<iostream.h>#include<fstream.h>voidmain(){charch;

ifstreamf1("d:test");if(!f1){cout<<"cannotopen'test'forinput.";return;}ofstreamf2("d:testnew");if(!f2){cout<<"cannotopentestnewforouput.";return;}while(f1&&f1.get(ch))f2.put(ch);f1.close();f2.close();cout<<"Itisover!\n";}ifstreamf1ch10.5.3文本文件#include<iostream.h>#include<fstream.h>voidmain(){charch;ifstreamf1("d:test");if(!f1){cout<<"cannotopen'test'forinput.";return;}

ofstreamf2("d:testnew");if(!f2){cout<<"cannotopentestnewforouput.";return;}while(f1&&f1.get(ch))f2.put(ch);f1.close();f2.close();cout<<"Itisover!\n";}ifstreamf1chofstreamf210.5.3文本文件例11-16把文件d:test复制到文件d:testnew中#include<iostream.h>#include<fstream.h>voidmain(){charch;ifstreamf1("d:test");if(!f1){cout<<"cannotopen'test'forinput.";return;}ofstreamf2("d:testnew");if(!f2){cout<<"cannotopentestnewforouput.";return;}

while(f1&&f1.get(ch))f2.put(ch);f1.close();f2.close();cout<<"Itisover!\n";}ifstreamf1ofstreamf2ch10.5.3文本文件例10-16把文件d:test复制到文件d:testnew中#include<iostream.h>#include<fstream.h>voidmain(){charch;ifstreamf1("d:test");if(!f1){cout<<"cannotopen'test'forinput.";return;}ofstreamf2("d:testnew");if(!f2){cout<<"cannotopentestnewforouput.";return;}while(f1&&f1.get(ch))f2.put(ch);

f1.close();f2.close();cout<<"Itisover!\n";}ifstreamf1ofstreamf2ch10.5.3文本文件例11-16把文件d:test复制到文件d:testnew中#include<iostream.h>#include<fstream.h>voidmain(){charch;ifstreamf1("d:test");if(!f1){cout<<"cannotopen'test'forinput.";return;}ofstreamf2("d:testnew");if(!f2){cout<<"cannotopentestnewforouput.";return;}while(f1&&f1.get(ch))f2.put(ch);

f1.close();f2.close();cout<<"Itisover!\n";}ifstreamf1ofstreamf2ch10.5.3文本文件例11-16把文件d:test复制到文件d:testnew中#include<iostream.h>#include<fstream.h>voidmain(){charch;ifstreamf1("d:test");if(!f1){cout<<"cannotopen'test'forinput.";return;}ofstreamf2("d:testnew");if(!f2){cout<<"cannotopentestnewforouput.";return;}while(f1&&f1.get(ch))f2.put(ch);f1.close();f2.close();cout<<"Itisover!\n";}10.5.3文本文件例11-16把文件d:test复制到文件d:testnew中#include<fstream.h>voidmain(){charstr[]="\tNewstring";ofstreamf2("d:testnew",ios::app); //追加方式

if(!f2){cout<<"cannotopentestnewforouput.";return;}

f2<<str;

//插入字符串

f2.close();}例11-17在文件d:testnew的末尾添加字符串插入串10.5.3文本文件例11-18建立一个包含学生学号、姓名、成绩的文本文件#include<iostream.h>#include<fstream.h>#include<stdlib.h>voidmain(){charfileName[30],name[30];intnumber,score;ofstreamoutstuf; cout<<"Pleaseinputthenameofstudentsfile:\n";cin>>fileName; outstuf.open(fileName,ios::out);if(!outstuf){cerr<<"Filecouldnotbeopen."<<endl;abort();}outstuf<<"学生成绩文件\n";cout<<"Inputthenumber,name,andscore:(EnterCtrl-Ztoendinput)\n?";while(cin>>number>>name>>score){outstuf<<number<<''<<name<<''<<score<<'\n';cout<<"?";}outstuf.close();}10.5.3文本文件#include<iostream.h>#include<fstream.h>#include<stdlib.h>voidmain(){charfileName[30],name[30];intnumber,score;

ofstreamoutstuf;

cout<<"Pleaseinputthenameofstudentsfile:\n";cin>>fileName; outstuf.open(fileName,ios::out); if(!outstuf) {cerr<<"Filecouldnotbeopen."<<endl;abort();}outstuf<<"学生成绩文件\n"; cout<<"Inputthenumber,name,andscore:(EnterCtrl-Ztoendinput)\n?";while(cin>>number>>name>>score){outstuf<<number<<''<<name<<''<<score<<'\n';cout<<"?";}outstuf.close(); }//建立输出文件流对象10.5.3文本文件例11-18建立一个包含学生学号、姓名、成绩的文本文件#include<iostream.h>#include<fstream.h>#include<stdlib.h>voidmain(){charfileName[30],name[30];intnumber,score;ofstreamoutstuf;

cout<<"Pleaseinputthenameofstudentsfile:\n";

cin>>fileName;

outstuf.open(fileName,ios::out); if(!outstuf) {cerr<<"Filecouldnotbeopen."<<endl;abort();}outstuf<<"学生成绩文件\n"; cout<<"Inputthenumber,name,andscore:(EnterCtrl-Ztoendinput)\n?";while(cin>>number>>name>>score){outstuf<<number<<''<<name<<''<<score<<'\n';cout<<"?";}outstuf.close();}//建立输出文件流对象//输入文件名10.5.3文本文件例11-18建立一个包含学生学号、姓名、成绩的文本文件#include<iostream.h>#include<fstream.h>#include<stdlib.h>voidmain(){charfileName[30],name[30];intnumber,score;ofstreamoutstuf; cout<<"Pleaseinputthenameofstudentsfile:\n";cin>>fileName;

outstuf.open(fileName,ios::out);

if(!outstuf) {cerr<<"Filecouldnotbeopen."<<endl;abort();}outstuf<<"学生成绩文件\n"; cout<<"Inputthenumber,name,andscore:(EnterCtrl-Ztoendinput)\n?";while(cin>>number>>name>>score){outstuf<<number<<''<<name<<''<<score<<'\n';cout<<"?";}outstuf.close(); }//建立输出文件流对象//输入文件名//连接文件,指定打开方式10.5.3文本文件例11-18建立一个包含学生学号、姓名、成绩的文本文件#include<iostream.h>#include<fstream.h>#include<stdlib.h>voidmain(){charfileName[30],name[30];intnumber,score;ofstreamoutstuf; cout<<"Pleaseinputthenameofstudentsfile:\n";cin>>fileName; outstuf.open(fileName,ios::out);

if(!outstuf)

{cerr<<"Filecouldnotbeopen."<<endl;abort();}outstuf<<"学生成绩文件\n"; cout<<"Inputthenumber,name,andscore:(EnterCtrl-Ztoendinput)\n?";while(cin>>number>>name>>score){outstuf<<number<<''<<name<<''<<score<<'\n';cout<<"?";}outstuf.close();}//建立输出文件流对象//输入文件名//连接文件,指定打开方式//调用重载算符函数测试流10.5.3文本文件例11-18建立一个包含学生学号、姓名、成绩的文本文件#include<iostream.h>#include<fstream.h>#include<stdlib.h>voidmain(){charfileName[30],name[30];intnumber,score;ofstreamoutstuf; cout<<"Pleaseinputthenameofstuden

温馨提示

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

最新文档

评论

0/150

提交评论