第10章 输入输出流

  • 格式:doc
  • 大小:47.00 KB
  • 文档页数:6

第10章 输入/输出流 10.1 选择题 1.下列类中( ac)不是输入输出流类iostream的派生类。 (a)fstream (b)ofstream (c)strstream (d)ostrstream 2.在下列选项中( d )是ostream类的对象。 (a)cin (b)cerr (c)clog (d)cout 3.read函数的功能是从输入流中读取( c )。 (a)一个字符 (b)当前字符 (c)一行字符 (d)指定若干个字符 4.下列选项中,用于清除基数格式位设置以十六进制输出的语句是( b )。 (a) cout<(b) cout<(c) cout<(d) cin >>setf( ios::hex,ios::basefield); 5.下列格式控制符,在iostream.h中定义的是( a,d ),在iomanip.h中定义的是( )。 (a)endl (b)setfill (c)setw (d)oct 6.下列串流类,在strstream.h中定义的是( b,d ),在sstream.h中定义的是( )。 (a)istringstream (b)istrstream (c)ostringstream (d)ostrstream 7. 包含类fstream定义的头文件是( a )。 (a)fstream.h (b)ofstream.h (c)ifstream.h (d)iostream.h 8.以写的方式打开文件 ” D:\file.dat”,错误的语句是(c )。 (a) ifstream infile( “D:\file.dat”, ios::in ); (b) ifstream infile( “D:\\file.dat”, ios::in); (c) ofstream infile( “D:\file.dat”, ios::out); (d) fstream infile( “D:\\file.dat”, ios::in|ios::out ); 9.假定已定义浮点型变量data,以二进制方式把data的值写入输出文件流对象outfile中去,正确的语句是( c )。 (a) outfile.write(( float* ) &data , sizeof( float )); (b) outfile.write(( float* ) &data , data ); (c) outfile.write(( char* ) &data , sizeof( float )); (d) outfile.write(( char* ) &data , data );

10.2 阅读下列程序,写出执行结果 1. #include < iostream.h > void main() { double x = 123.456; cout.width( 10 ); cout.setf( ios :: dec, ios :: basefield ); cout << x << endl; cout.setf( ios :: left ); cout << x << endl; cout.width( 15 ); cout.setf( ios::right , ios::left ); cout << x << endl; cout.setf( ios::showpos ); cout << x << endl; cout << -x << endl; cout.setf( ios :: scientific ); cout << x << endl; } 答案: 123.456 123.456 123.456 +123.456 -123.456 +1.234560e+002

2. #include

void main() { double x = 123.45678; cout.width( 10 ); cout << ( "#" ); cout << x << endl; cout.precision( 5 ); cout << x << endl; cout.setf( ios::showpos ); cout << x << endl; cout.setf( ios::scientific ); cout << x << endl; } 答案: #123.457 123.46 +123.457 +1.23457e+002

3. #include

#include void main() { double x = 123.456789; cout << setiosflags( ios::fixed | ios::showpos ) << x < cout << setw( 12 ) << setiosflags( ios::right ); cout<< setprecision( 3 ) << -x << endl; cout << resetiosflags( ios::fixed | ios::showpos ) << setiosflags( ios::scientific ); cout << setprecision( 5 ) << x << endl; } 答案:

10.3 编程题 1.以表格形式输出当x = 1°,2°,…,10°时的sin(x)、cos(x)和tg(x)的值,要求输出时数据的宽度为10、左对齐和保留小数点后5位。 解答: #include #include #include using namespace std;

void main(){ int i=1; cout.setf(ios::left); cout<<<"cos(x)"

cout

cout<4)

3.把由第2题建立的文本文件读出,显示在屏幕上并统计该文件的行数。 解答: #include #include void main() { ifstream out("D://filename.txt"); if(!out) { cout<<"open failed!"<} int i,count=0,row=1; char c; while(out.get(c)) {

cout

} 4.建立某单位职工通讯录二进制文件,文件中的每个记录包括职工编号、姓名、电话号码、邮政编码和住址。 #include #include #include using namespace std; struct worker { int number; string name; int callnumber; int post; string address; };

void main() { int n,i; worker list; fstream out;

out.open("D://list.txt",ios::out|ios::in); if(!out) { cout<<"open failed!"<} cout<<"input the number of wokers:"; cin>>n;