教学内容: 1 、 c++ 流概述 2 、标准输入输出的预定义流对象 3 、文件流 ...
DESCRIPTION
C++ 流. 教学内容: 1 、 C++ 流概述 2 、标准输入输出的预定义流对象 3 、文件流 4 、字符串流 5 、输入输出的格式控制. c++ 流是指信息在内存与外部设备之间传递流动的过程。 c++ 流: 1 )标准 I/o 流 2 )文件 I/o 流 3 )字符串 I/o 流. 在 c++ 中输入输出是通过流来实现的。 c++ 将与输入和输出有关的操作定义为一个类体系,放在一个系统库里,以备调用。这个输入输出操作的类体系叫做流类,提供这个流类实现的系统库叫流类库。 - PowerPoint PPT PresentationTRANSCRIPT
教学内容:1 、 C++ 流概述 2 、标准输入输出的预定义流对象 3 、文件流 4 、字符串流 5 、输入输出的格式控制
在 c++ 中输入输出是通过流来实现的。
c++ 将与输入和输出有关的操作定义为一个类体系,放在一个系统库里,以备调用。这个输入输出操作的类体系叫做流类,提供这个流类实现的系统库叫流类库。
c++ 的流类库有几个 I/O 操作的基础类和几个特定种类的源和目标的 I/O 操作的类组成。
c++ 流是指信息在内存与外部设备之间传递流动的过程。
c++ 流:
1 )标准 I/o 流
2 )文件 I/o 流
3 )字符串 I/o 流
iosstreambuf
istream ostream
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=13 the 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 中定义的对象,可以直接将其插入流中,不必单独调用。
用控制符控制格式输出 #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=13 the 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 操作之前要定义相应的流并规定打开方式,操作之后要关闭响应的流。