第九章 c++ 流

24
第第第 第第第 C++ C++ 第第第第第第第第 第第第第第第第第

Upload: thalia

Post on 05-Jan-2016

73 views

Category:

Documents


0 download

DESCRIPTION

第九章 C++ 流. 计算机与通信学院. 一、四个系统头文件 1 、 iostream.h :包含 ios,iostream,istream,ostream 以及 endl 等操纵符的定义。 2 、 fstream.h : 包含 fstream, ifstream,ofstream, fstreambase 等类的定义以及 iostream.h 中的所有内容。 3 、 strstrea.h :包含 strstream, istrstream, - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: 第九章    C++ 流

第九章 第九章 C++C++ 流流计算机与通信学院计算机与通信学院

Page 2: 第九章    C++ 流

一、四个系统头文件一、四个系统头文件11 、、 iostream.hiostream.h :包含:包含 ios,iostream,istream,ostreamios,iostream,istream,ostream

以及以及 endlendl 等操纵符的定义。等操纵符的定义。22 、、 fstream.hfstream.h : 包含: 包含 fstream, ifstream,ofstream, fstream, ifstream,ofstream,

fstreambasefstreambase 等类的定义以及等类的定义以及 iostream.hiostream.h 中的所有内容。中的所有内容。33 、、 strstrea.hstrstrea.h :包含:包含 strstream, istrstream,strstream, istrstream,

ostrstream,strstreambaseostrstream,strstreambase 等类的定等类的定 义以及 义以及 iostream.hiostream.h 中的所有内容。中的所有内容。44 、、 iomanip.hiomanip.h : 包含: 包含 setwsetw 等大多数操纵符的定义以及等大多数操纵符的定义以及 iostream.hiostream.h 中的所有内容。中的所有内容。

Page 3: 第九章    C++ 流

二、四个预定义流对象二、四个预定义流对象11 、、 cin cin :标准输入 。 :标准输入 。 22 、、 coutcout :标准输出。:标准输出。33 、、 cerrcerr :标准出错信息输出。:标准出错信息输出。44 、、 clogclog :带缓冲的标准出错信息输出。:带缓冲的标准出错信息输出。

Page 4: 第九章    C++ 流

三、输入输出的格式控制三、输入输出的格式控制11 、、 decdec :整数按十进制输出。:整数按十进制输出。22 、、 octoct : 整数按八进制输出。: 整数按八进制输出。33 、、 hexhex :整数按十六进制输出。:整数按十六进制输出。44 、、 endlendl :回车换行。:回车换行。55 、、 setwsetw :宽度设置。:宽度设置。66 、、 ws:ws: 输入时跳过数据前的空白字符输入时跳过数据前的空白字符 ,,

停 在第一个非空白字符处。停 在第一个非空白字符处。77 、、 ends:ends: 插入字符串结束符’ 插入字符串结束符’ \0’\0’ 。。

Page 5: 第九章    C++ 流

操纵符 : endl, setw

输入输出的格式控制 (p312 表 )

格式控制标志的设置

long ios::setf(long flag, long mask)

long ios::setf(long flag)

long ios::unsetf(long flag)

Page 6: 第九章    C++ 流

输入输出的数制状态控制

dec io.setf(ios::dec, ios::basefield)

io.unsetf(ios::basefield)

oct io.setf(ios::oct, ios::basefield)

hex io.setf(ios::hex, ios::basefield)

输入输出的宽度控制:

setw(int n) io.width(n)

Page 7: 第九章    C++ 流

输入输出精度控制

setprecision(int n)

io.precision(n)

浮点数输出方式控制

resetiosflag(ios::floatfield)

o.unsetf(ios::floatfield)

Page 8: 第九章    C++ 流

对齐方式控制

o.setf(ios::left, ios::adjustfield)

o.setf(ios::right, ios::adjustfield)

o.unsetf(ios::adjustfield)

o.setf(ios::internal, ios::adjustfield)

填充字符控制

setfill(char c)

io.fill(c)

Page 9: 第九章    C++ 流

非负数的符号表示方式控制

showpos

o.setf(ios::showpos)

插入换行符

endl

o.put(‘\n’); o.flush()

插入字符串结束符

ends

o.put(‘\0’)

Page 10: 第九章    C++ 流

前导空白字符处理方式控制

skipws

i.setf(ios::skipws)

写缓方式控制

unitbuf

o.setf(ios::unitbuf)

跳过前导空白字符

ws

Page 11: 第九章    C++ 流

#include<iostream.h>

void main()

{ int x=35,y=391,z=1024;

cout<<x<<‘ ‘<<y<<‘ ’<<z<<endl;

cout<<oct<<x<<‘ ‘<<y<<‘ ‘<<z<<endl;

cout<<hex<<x<<‘ ‘<<y<<‘ ‘<<z<<end;

}

Page 12: 第九章    C++ 流

#include<iomanip.h>

Void main()

{ double x=5671234.56,y=-3.1415926;

cout<<x<<‘,’<<y<<endl;

cout<<setprecision(7)<<setw(18)<<x<<‘,’<<

setw(18)<<y<<endl;

cout<<setfill(‘*’)<<setw(18)<<x<<‘,’<<

setw(18)<<y<<endl;

cout<<setf(ios::left, ios::adjustfield);

cout<<setw(18)<<x<<‘,’<<setw(18)<<y<<endl;}

Page 13: 第九章    C++ 流

四、利用文件流对象对文件进行访问操作四、利用文件流对象对文件进行访问操作11 、定义文件流对象、定义文件流对象如:如: ifstream f1;//f1ifstream f1;//f1 为输入文件流类对为输入文件流类对

象象 ofstream f2; //f2ofstream f2; //f2 为输出文件流类对为输出文件流类对

象象 fstream f3; //f3fstream f3; //f3 为输入输出文件流类为输入输出文件流类

对象对象

Page 14: 第九章    C++ 流

22 、打开文件、打开文件(( 11 )) openopen 成员函数成员函数 每个文件流类都有一个每个文件流类都有一个 openopen 成员函数, 成员函数,

格式为:格式为:void open(const charvoid open(const char ** fname,int modfname,int mod

e);e); 其中其中 :fname:fname 表示要打开文件的文件名;表示要打开文件的文件名; (文件名可以带盘符和路径名)(文件名可以带盘符和路径名) modemode 表示打开方式(见表示打开方式(见 P321P321 ))

Page 15: 第九章    C++ 流

如 如 (1)ofstream fout;(1)ofstream fout;

fout.open(fout.open(””a:\\xxk.dat a:\\xxk.dat ””););

(2)ifstream fin;(2)ifstream fin;

fin.open(fin.open(”” a:\\xxk.dat a:\\xxk.dat ””););

(3)ofstream ofs;(3)ofstream ofs;

fout.open(fout.open(”” a:\\xxk.dat a:\\xxk.dat ””,ios ap∷,ios ap∷p);p);

(4)fstream fio;(4)fstream fio;

fio.open(“a:\xxk.ran”,fio.open(“a:\xxk.ran”,

ios::in|ios::out|ios::binary)ios::in|ios::out|ios::binary)

Page 16: 第九章    C++ 流

(( 22 )构造函数)构造函数 每个文件流类中,既定义无参构造函数,每个文件流类中,既定义无参构造函数,

又定义有参构造函数,并且所带参数与又定义有参构造函数,并且所带参数与 oopenpen 成员函数所带参数相同。成员函数所带参数相同。

(( 33 )两种等价表示)两种等价表示如: 如: ofstream fout;ofstream fout;

fout.open(fout.open(””a:\\xxk.dat a:\\xxk.dat ””););

ofstream fout (ofstream fout (””a:\\xxk.dat a:\\xxk.dat ””););

33 、关闭文件:、关闭文件: close( )close( )

Page 17: 第九章    C++ 流

文件流状态的判定 is_open( )

good( )

fail( )

bad( )

eof( )

//input 为一流对象 if(!input) if(input.fail( ))

if(input) if(input.good( ))

Page 18: 第九章    C++ 流

向文本文件输出数据向文本文件输出数据 ostream& operator<<(ostream& operator<<( 简单类型简单类型 ))

ostream& put(char)ostream& put(char)

Page 19: 第九章    C++ 流

例:向例:向 aa 盘上的盘上的 wr1.datwr1.dat 文件输出文件输出 0~20~200 之间的整数,含之间的整数,含 00 和和 2020 在内。在内。

#include<stdlib.h> #include<stdlib.h> #include<fstream.h>#include<fstream.h>void main( )void main( ){ofstream f1({ofstream f1(””a:wr1.dat a:wr1.dat ””););if (!f1)if (!f1){cerr<< {cerr<< ”” a:wr1.dat file not open a:wr1.dat file not open ”” <<end <<end

l; exit(1)}l; exit(1)}for(int i=0;i<21;i++) f1<<i<< for(int i=0;i<21;i++) f1<<i<< ” ”” ”;;f1.close( ); }f1.close( ); }

Page 20: 第九章    C++ 流

从文本文件输入数据从文本文件输入数据 istream& operator>>(istream& operator>>( 简单类型简单类型 &)&)

int get( )int get( )

istream& get(char&)istream& get(char&)

istream& getline(char* buffer, int len, cistream& getline(char* buffer, int len, char=‘\n’)har=‘\n’)

Page 21: 第九章    C++ 流

#include<iomanip.h>#include<fstream.h>void JB(char * fname) // 可把以 fname 所指字符串作为文件标识符的文件称为 fname 文件, // 假定该文件中保存着一批字符串,每个字符串的长度均小于 20 。 { ifstream fin(fname) ; char a[20] ; int i = 0 ; while(fin>>a) { cout<<a<<endl ; i++ ; } fin.close() ; cout<<”i =” <<i<<endl , }

Page 22: 第九章    C++ 流

istream& read(char* buffer, int len)istream& read(char* buffer, int len)

ostream& write(const char* buffer, int len)ostream& write(const char* buffer, int len)

Page 23: 第九章    C++ 流

#include<iostream.h>

#include<fstream.h>

Void main()

{ ifstream f1(“d:\shf1.dat”,ios::in|ios::nocreate|ios::binary);

int x,max,min;

float mean;

f1.read((char*)&x,sizeof(x));

mean =max=min=x;

int n=1;

Page 24: 第九章    C++ 流

while(!f1.eof()){ f1.read((char*)&x,sizeof(x)); if(x>max) max=x; else if(x<min) min=x; mean+=x; n++;}mean/=n;cout<<“ 最大数:” <<max<<endl;cout<<“ 最小数:” <<min<<endl;cout<<“ 平均数:” <<mean<<endl;f1.close();}