240 likes | 393 Views
第九章 C++ 流. 计算机与通信学院. 一、四个系统头文件 1 、 iostream.h :包含 ios,iostream,istream,ostream 以及 endl 等操纵符的定义。 2 、 fstream.h : 包含 fstream, ifstream,ofstream, fstreambase 等类的定义以及 iostream.h 中的所有内容。 3 、 strstrea.h :包含 strstream, istrstream,
E N D
第九章 C++流 计算机与通信学院
一、四个系统头文件 1、iostream.h:包含ios,iostream,istream,ostream 以及endl等操纵符的定义。 2、fstream.h: 包含fstream, ifstream,ofstream, fstreambase等类的定义以及 iostream.h中的所有内容。 3、strstrea.h:包含strstream, istrstream, ostrstream,strstreambase等类的定 义以及 iostream.h中的所有内容。 4、iomanip.h: 包含setw等大多数操纵符的定义以及 iostream.h中的所有内容。
二、四个预定义流对象 1、cin :标准输入 。 2、cout:标准输出。 3、cerr:标准出错信息输出。 4、clog:带缓冲的标准出错信息输出。
三、输入输出的格式控制 1、dec:整数按十进制输出。 2、oct: 整数按八进制输出。 3、hex:整数按十六进制输出。 4、endl:回车换行。 5、setw:宽度设置。 6、ws:输入时跳过数据前的空白字符,停 在第一个非空白字符处。 7、ends:插入字符串结束符’\0’。
操纵符: endl, setw 输入输出的格式控制(p312表) 格式控制标志的设置 long ios::setf(long flag, long mask) long ios::setf(long flag) long ios::unsetf(long flag)
输入输出的数制状态控制 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)
输入输出精度控制 setprecision(int n) io.precision(n) 浮点数输出方式控制 resetiosflag(ios::floatfield) o.unsetf(ios::floatfield)
对齐方式控制 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)
非负数的符号表示方式控制 showpos o.setf(ios::showpos) 插入换行符 endl o.put(‘\n’); o.flush() 插入字符串结束符 ends o.put(‘\0’)
前导空白字符处理方式控制 skipws i.setf(ios::skipws) 写缓方式控制 unitbuf o.setf(ios::unitbuf) 跳过前导空白字符 ws
#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; }
#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;}
四、利用文件流对象对文件进行访问操作 1、定义文件流对象 如:ifstream f1;//f1为输入文件流类对象 ofstream f2; //f2为输出文件流类对象 fstream f3; //f3为输入输出文件流类对象
2、打开文件 (1)open成员函数 每个文件流类都有一个open成员函数, 格式为: void open(const char*fname,int mode); 其中:fname表示要打开文件的文件名; (文件名可以带盘符和路径名) mode表示打开方式(见P321)
如 (1)ofstream fout; fout.open(”a:\\xxk.dat ”); (2)ifstream fin; fin.open(” a:\\xxk.dat ”); (3)ofstream ofs; fout.open(” a:\\xxk.dat ”,ios∷app); (4)fstream fio; fio.open(“a:\xxk.ran”, ios::in|ios::out|ios::binary)
(2)构造函数 每个文件流类中,既定义无参构造函数,又定义有参构造函数,并且所带参数与open成员函数所带参数相同。 (3)两种等价表示 如: ofstream fout; fout.open(”a:\\xxk.dat ”); ofstream fout (”a:\\xxk.dat ”); 3、关闭文件:close( )
文件流状态的判定 is_open( ) good( ) fail( ) bad( ) eof( ) //input为一流对象 if(!input) if(input.fail( )) if(input) if(input.good( ))
向文本文件输出数据 ostream& operator<<(简单类型) ostream& put(char)
例:向a盘上的wr1.dat文件输出0~20之间的整数,含0和20在内。例:向a盘上的wr1.dat文件输出0~20之间的整数,含0和20在内。 #include<stdlib.h> #include<fstream.h> void main( ) {ofstream f1(”a:wr1.dat ”); if (!f1) {cerr<< ” a:wr1.dat file not open ” <<endl; exit(1)} for(int i=0;i<21;i++) f1<<i<< ” ”; f1.close( ); }
从文本文件输入数据 istream& operator>>(简单类型&) int get( ) istream& get(char&) istream& getline(char* buffer, int len, char=‘\n’)
#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, }
istream& read(char* buffer, int len) • ostream& write(const char* buffer, int len)
#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;
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(); }