例6.1.1 static filebuf stdin filebuf. stdin-filebuf new filebuf (F_stdin) /F_ stdin:标准输入所用的文件描述字 cin= stdin filebuf //将缓冲区与cin相连 cin. tie(& cout) /将cin与cout联系起来
例6.1.1 static filebuf * stdin_filebuf; stdin_filebuf = new filebuf(F_stdin); // F_stdin:标准输入所用的文件描述字 cin = stdin_filebuf; // 将缓冲区与cin相连 cin.tie(& cout); // 将cin与cout联系起来
例612 #include <iostream .h> void maino int i=0 char buf[20] while(i<20&& cin. get(buf)&& buf[!="n) buf[=10 cout<<buf<<endl
例6.1.2 #include <iostream.h> void main() { int i=0; char buf[20]; while(i<20 && cin.get(buf[i]) && buf[i]!='\n') i++; buf[i]='\0'; cout<<buf<<endl; }
例613 include <iostream.h> void maino int C while((c=cin. get()=EOF cout<<Ok!\n
例6.1.3 #include <iostream.h> void main() { int c; while((c=cin.get())!=EOF); cout<<"Ok!\n"; }
类 stream的定义 1)插入运算符“<<”定义在 ostream类中,并 且对所有的预定义类型都给出了其重载定义 2)对于 ostream类中定义的其他成员函数, 流对象可以使用分量运算符引用
类ostream的定义 ❖ 1)插入运算符“<<”定义在ostream类中,并 且对所有的预定义类型都给出了其重载定义 ❖ 2)对于ostream类中定义的其他成员函数, 流对象可以使用分量运算符引用
例614 #include <iostream. h> void maino int fun( cout<<"The value of function fun is: "<<fun(<< endl; // int fun( cout <<Sorry, I am first! In return 250
例6.1.4 #include <iostream.h> void main() { int fun(); cout<<"The value of function fun is: "<< fun()<< endl;//(1) } int fun() { cout << "Sorry, I am first! \n"; return 250; }