300 likes | 486 Views
C++ 流. 教学内容: 1 、 C++ 流概述 2 、标准输入输出的预定义流对象 3 、文件流 4 、字符串流 5 、输入输出的格式控制. c++ 流是指信息在内存与外部设备之间传递流动的过程。 c++ 流: 1 )标准 I/o 流 2 )文件 I/o 流 3 )字符串 I/o 流. 在 c++ 中输入输出是通过流来实现的。 c++ 将与输入和输出有关的操作定义为一个类体系,放在一个系统库里,以备调用。这个输入输出操作的类体系叫做流类,提供这个流类实现的系统库叫流类库。
E N D
C++流 教学内容: 1、 C++流概述 2、标准输入输出的预定义流对象 3、文件流 4、字符串流 5、输入输出的格式控制
c++流是指信息在内存与外部设备之间传递流动的过程。c++流是指信息在内存与外部设备之间传递流动的过程。 c++流: 1)标准I/o流 2)文件I/o流 3)字符串I/o流 在c++中输入输出是通过流来实现的。 c++将与输入和输出有关的操作定义为一个类体系,放在一个系统库里,以备调用。这个输入输出操作的类体系叫做流类,提供这个流类实现的系统库叫流类库。 c++的流类库有几个I/O操作的基础类和几个特定种类的源和目标的I/O操作的类组成。
streambuf ios ostream istream iostream iostream流类库的基本类等级
一、I/O标准流类 标准流的设备名 标准流对象在标准头文件iostream.h中定义。 (1)cout是标准输出流类ostream的对象。ostream流类在标准头文件 iostream.h中定义。 (2)cin是标准输入流类istream的对象。istream流类在标准头文件 iostream.h中定义。 (3)cerr是标准错误流对象。写到cerr上的信息是不能被重新定向的, 故只能在屏幕上显示。(非缓冲方式) (4)clog与标准错误输出设备相关联(缓冲方式)
例: #include "iostream.h" void main(){ cerr<<"You r Wrong!!!\n"; clog<<"Now u r right\n"; //clog.flush(); int i; cin>>i; }
例、标准错误流对象的使用 #include <iostream.h> void fn(int a,int b){ if(b= =0) cerr<<"zero encounted." <<"The message cannot be redirected."; //除数为0时,输出错误信息 else cout<<a/b<<endl; } void main(){ fn(20,2); fn(20,0); }
二、文件流类 1、ofstream(输出文件流类),ifstream(输入文件流类)和fstream(文件流 类)在fstream.h中定义。 2、定义一个文件流类对象时,应指明文件名和打开方式。 ofstream流类用于定义输出文件流对象,构造函数为: ofstream::ofstream(char * fname,int mode=ios::out, int prot=filebuf::openprot); 其中:fname指打开的文件名; mode指打开方式,涵义如下:
prot指文件保护方式,涵义如下: 文件打开方式: (1)ofstream bfile("binfile",ios::binary|ios::ate); (2)ifstream myinf("abc.dat",ios::nocreate); (3)fstream myinout("abc.dat",ios::in|ios::out);
例、文件打开方式的使用 #include<iostream.h> #include<fstream.h> void fn(){ ofstream myf("c:\\temp\\myname"); //ios::out|ios::trunc方式打开 myf<<"what's your name?\n" <<"I am a student.\n" <<"how are you!\n"; } void main(){ fn(); }
3、成员函数fail()可测定文件打开是否成功。 fail()值为1(若文件打开失败),值为0(若文件打开成功)。 成员函数fail()的使用: #include<iostream.h> void fn(){ ofstream myf("c:\\temp\\myname"); //ios::out|ios::trunc方式打开 if(myf.fail()){ cerr<<"error opening file myname\n"; return; } myf<<"what's your name?\n" <<"I am a student.\n" <<"how are you!\n"; } void main(){ fn(); }
三、串流类: 1、ostrstream(输出串流类),istrstream(输入串流类)和strstream(串 流类)在strstrea.h中定义。 2、类istrstream用于定义输入串流对象,常用构造函数为: istrstream:: istrstream(const char * str); istrstream:: istrstream(const char * str,int size); 其中size指数组大小。 3、otrstream用于执行串流输出, 常用构造函数为: otrstream ::otrstream(char *,int size,int=ios::out); 其中:第三个参数为打开方式。
例题:串流类的使用: #include <iostream.h> #include<strstrea.h> void main(){ char str[100]="I am a student.\n"; char a,b; char c[10]; istrstream ai(str); //str为输入设备 ai>>a>>b>>c; //自输入设备输入一个字符 cout<<a<<" "<<b<<c<< " " <<endl; //输出一个字符 }
例题:串流类的使用 #include <iostream.h> #include<strstrea.h> char * parsestring(char * pstring){ istrstream inp(pstring,0); int anumber; float balance; inp>>anumber>>balance; char * pbuffer=new char[128]; ostrstream outp(pbuffer,128); outp<<"anumber="<<anumber<<",balance="<<balance<<endl; cout<<pbuffer; return pbuffer; //anything wrong? } void main() { char * str="1234 100.35"; char * pbuf=parsestring(str); cout<<pbuf<<endl; delete []pbuf; } 结果:anumber=1234,balance=100.35
四、输入输出格式控制 1.格式控制标志设置 1)格式控制的标志位含义 skipws:跳过输入中的空白,可用于输入 left: 左对齐输出,可用于输出 right: 右对齐输出,可用于输出 internal dec oct hex fixed
2)控制输入输出格式的成员函数 setf(long flags): 设置状态标志位flags unsetf(long flags): flags(): 测试状态标志 flags(long flags) 设置标志flags,并返回设置前标志 width(): 返回当前宽度值 width(int w): 设置宽度 precision(int p): 设置小数点位数 fill(): 返回当前填充字 fill(char ch): 设置填充字符 例子: istream iobj; ostream oobj; iobj.set(ios::skipws); osobj.setf(ios::left); 一般,我们使用最多的输入输出流是cin,cout
例、用流对象的成员函数控制格式输出 #include <iostream.h> void fn(float interest,float amount){ cout<<"RMB amount="; cout.precision(2); cout<<amount; cout<<"\nthe interest="; cout.precision(4); cout<<interest<<endl; } void main(){ float f1=29.41560067; float f2=12.567188; fn(f1,f2); } 结果:RMB amount=13the interest=29.42
#include "iostream.h" void main(){ cout<<"之前"<<567<<" "<<567.89<<endl; cout.setf(ios::showpos|ios::scientific); //正号、科学计数法 cout<<"之后"<<567<<" "<<567.89<<endl; cout.unsetf(ios::showpos|ios::scientific); cout<<"清除"<<567<<" "<<567.89<<endl; cout.width(20); cout.fill('@'); cout<<"域宽20"<<567<<" "<<567.89<<endl; } 注意: 1、设置多项标记时,可用"|"分割 2、flags与setf的区别是,前者覆盖以前的状态字,后者在原有基础上追 加。
#include "iostream.h" #include "math.h" void main(){ double x; cout.precision(4); cout<<" x sqrt(x) x^2\n\n"; for (x=1.0;x<=20.0;x++){ cout.width(8); // cout<<x<<' '; cout.width(8); cout<<sqrt(x)<<' '; cout.width(8); cout<<x*x<<"\n"; } }
2、使用控制符 • C++预定义的操作符有: • dec: 十进制 • hex: 十六进制 • oct: 八进制 • ws: 跳过空白符 • endl: 换行 "\n" • ends: 插入一个空格 • flush: 刷新 • setbase(int n): 设置转换基数 • resetiosflags(long f): 标记复位 • setiosflags(long f): 设置标记 • setfill(int c): 设置填充字符 • setprecision(int n): 小数位 • setw(int n) : 域宽
控制符(manipulators)是在头文件iomanip.h中定义的对象,可以直接将其插入流中,不必单独调用。控制符(manipulators)是在头文件iomanip.h中定义的对象,可以直接将其插入流中,不必单独调用。 用控制符控制格式输出 #include <iostream.h> #include <iomanip.h> void fn(float interest,float amount){ cout<<"RMB amount="<<setprecision(2)<<amount; cout<<"\nthe interest="<<setprecision(4)<<interest<<endl; } void main(){ float f1=29.41560067; float f2=12.567188; fn(f1,f2); } 结果:RMB amount=13the interest=29.42
#include "iostream.h" #include "iomanip.h" void main(){ cout<<setw(10)<<123<<ends<<567<<endl; cout<<123<<setw(10)<<hex<<123<<endl; cout<<123<<setw(10)<<oct<<123<<endl; cout<<123<<setw(10)<<setbase(0)<<123<<endl; cout<<123<<setw(10)<<setbase(8)<<123<<endl; cout<<123<<setw(10)<<setbase(16)<<123<<endl; int i; cin>>i; }
常用控制符及对应的成员函数 其中setw(n)和width(n)为一次性的。
例题:用流对象的成员函数控制格式输出 #include <iostream.h> #include <iomanip.h> void main(){ cout.width(8); cout<<10<<20<<endl; //cout<<setw(8)<<10<<setw(8)<<20<<endl; } 结果: 1020
例题: 用控制符控制格式输出 #include <iostream.h> #include <iomanip.h> void main(){ for(int n=1;n<8;n++) cout<<setfill(' ')<<setw(n)<<" " <<setfill(‘*')<<setw(15-2*n)<<"m"<<endl; } 结果: ************* *********** ********* ******* ***** *** *
3、使用I/O成员函数输入输出 1)getline():读取一整行文本 函数原型:getline(char *line,int size,char='\n'); char='\n'为行结束标志。 如:getline(char *line,int size,char='x'); 该函数读到字符x为止。 例、如何读入一整行文本 #include <iostream.h> void main(){ char str[128]; cout<<"请输入任一行文本后按回车……"<<endl; cin.getline(str,sizeof(str)); cout<<"你输入的文本是:"<<endl<<str<<endl; }
例、如何读入以字符x为止的一行文本 #include <iostream.h> void main(){ char str[128]; cout<<"请输入任一行文本后按回车(其中包含字符x)……"<<endl; cin.getline(str,sizeof(str),'x'); cout<<"你输入的第一行文本是:"<<endl<<str<<endl; cin.getline(str,sizeof(str)); cout<<"你输入的第二行文本是:"<<endl<<str<<endl; }
2)get():读取一个字符 函数原型:char istream::get( ); 例、循环读入字符,直到用户键入y字符,或遇到ctrl-c(文件尾)。 #include <iostream.h> #include <ctype.h> void main(){ char letter; while(!cin.eof()) { letter=cin.get(); letter=toupper(letter); if(letter = = 'Y') { cout<<"遇到字符Y."<<endl; break; } cout<<letter<<endl; } }
3)get():输入一系列字符 函数原型:istream& istream::get(char *,int n,char delim='\n' ); 如: istream fin("abc.dat"); char buffer[80]; fin.get(buffer,80);
4) put():输出一个字符 例、利用put()输出一个字符 #include <iostream.h> void main(){ char letter; for(letter='a';letter<='z';letter++) cout.put(letter); }
课堂小结 1、c++系统中提供了一个功能完备的用于输入输出操作的流类库,用户可以非常方便地对各种设备进行数据输入输出操作。流类库中所有的流类由iostream.h、 fstream.h、 strstrea.h三个系统头文件包含着。 2、使用调用ios类中的成员函数或使用格式化操纵符可以实现对数据的格式化输入输出操作。 3、对磁盘文件进行I/O操作之前要定义相应的流并规定打开方式,操作之后要关闭响应的流。