c++流类库与输入输出习题答案

更新时间:2023-10-01 07:15:01 阅读量: 综合文库 文档下载

说明:文章内容仅供预览,部分内容可能不全。下载后的文档,内容与下面显示的完全一致。下载之前请确认下面内容是否您想要的,是否完整无缺。

1.概念填空题

1.1头文件iostream中定义了 4个标准流对象 cin , cout , cerr , clog 。其中标准输入流对象为 cin ,与 键盘 连用,用于输入; cout 为标准输出流对象,与显示器 连用,用于输出。

1.2用标准输入流对象cin与提取操作符>>连用进行输入时,将 空格 与 回车 当作分隔符,使用 get() 成员函数进行输入时可以指定输入分隔符。

1.3每一个输入输出流对象都维护一个 流格式状态字 ,用它表示流对象当前的格式状态并控制流的格式。C++提供了使用 格式控制 函数与 操作子 函数来控制流的格式的方法。 1.4 C++根据文件内容的 数据格式 可分为两类: 文本文件 和 二进制文件 。前者存取的最小信息单位为 字节 ,后者 记录 。

1.5文件输入是指从文件向 内存 读入数据;文件输出则指从 内存 向文件输出数据。文件的输入输出首先要 打开文件 ;然后 进行读写 ;最后 关闭文件 。 1.6文本文件是存储ASCII码字符的文件,文本文件的输入可用 cin 从输入文件流中提取字符实现。文本文件的输出可用 cout 将字符插入到输出文件流来实现。程序在处理文本文件时 需要 (需要/不需要)对数据进行转换。

1.7二进制文件是指直接将计算机内的数据不经转换直接保存在文件中。二进制文件的输入输出分别采用read()、write() 成员函数。 这两个成员函数的参数都是 2个,分别表示 读写缓冲区 和 字节数 。

1.8设定、返回文件读指针位置的函数分别为 seekg , tellg ;设定、返回文件写指针位置的函数分别为 seekp , tellp 。

2 简答题

2.1 为什么cin输入时,空格和回车无法读入?这时可改用哪些流成员函数? 2.2 文件的使用有它的固定格式,试做简单介绍。

2.3 在ios类中定义的文件打开方式中,公有枚举类型open_mode的各成员代表什么文件打开方式?

2.4 简述文本文件和二进制文件在存储格式、读写方式等方面的不同,各自的优点和缺点。 2.5 文本文件可以按行也可以按字符进行复制,在使用中为保证能完整复制要注意哪些问题?

2.6 文件的随机访问为什么总是用二进制文件,而不用文本文件? 2.7 怎样使用istream和ostream的成员函数来实现随机访问文件?

3.选择题

3.1要进行文件的输出,除了包含头文件iostream外,还要包含头文件(C )。 A.ifstream B.fstream C.ostream D.cstdio 3.2执行以下程序: char *str; cin>>str; cout<

若输入abcd 1234↙则输出(D)。 A.abcd B.abcd 1234 C.1234 D.输出乱码或出错 3.3执行下列程序: char a[200];

cin.getline(a,200,’ ‘);

cout<

若输入abcd 1234↙则输出(A)。 A.abcd B.abcd 1234 C.1234 D.输出乱码或出错 3.4定义char *p=”abcd”,能输出p的值(“abcd”的地址)的为(A /D ?)。 A.cout<<&p; B.cout<(p); 3.5以下程序执行结果(A)。 cout.fill(‘#’); cout.width(10);

cout<

3.6当使用ifstream定义一个文件流,并将一个打开文件的文件与之连接,文件默认的打开方式为(A)。 A.ios::in B.ios::out C.ios::trunc D.ios::binary 3.7从一个文件中读一个字节存于char c;正确的语句为(B)。 A.file.read(reinterpret_cast(&c), sizeof(c)); B.file.read(reinterpret_cast(&c), sizeof(c)); C.file.read((const char *)(&c), sizeof(c)); D.file.read((char *)c, sizeof(c);

3.8将一个字符char c=’A’写到文件中错误的语句为(D)。 A.file.write(reinterpret_cast(&c), sizeof(c)); B.file.write(reinterpret_cast(&c), sizeof(c)); C.file.write((char *)(&c), sizeof(c)); D.file.write((const char *)c, sizeof(c)); 3.9读文件最后一个字节(字符)的语句为(B)。 A.myfile.seekg(1,ios::end); B.myfile.seekg(-1,ios::end); c=myfile.get(); c=myfile.get(); C.myfile.seekp(ios::end,0); D.myfileseekp(ios::end,1); c=myfile.get(); c=myfile.get(); 3.10 read函数的功能是从输入流中读取(D )。

A.一个字符 B.当前字符 C.一行字符 D.指定若干字节

3.11 要求打开文件D:\\file.dat,并能够写入数据,正确的语句是( D )。 A.ifstream infile(\ B. ifstream infile(\ C. ofstream outfile(\

D.fstream infile(\

3.12 设己定义浮点型变量data,以二进制方式把data的值写入输出文件流对象outfile中去,正确的每句是( C )。

A. outfile.write((double*)&data, sizeof (double)); B.outfile.write((double*)&data, data);

C. outfile.write((char*)&data, sizeof (double)); D. outfile.write((char*)&data, data);

4.编程题

4.1 编程实现以下数据输入/输出:

(1)以左对齐方式输出整数,域宽为12。

(2)以八进制、十进制、十六进制输入/输出整数。

(3)实现浮点数的指数格式和定点格式的输入/输出,并指定精度。

(4)把字符串读入字符型数组变量中,从键盘输入,要求输入串的空格也全部读入,以回车

符结束。

(5)将以上要求用流成员函数和流操作子各做一遍。 Ios类成员函数实现 #include #include using namespace std; int main(){ long a=234; double b=2345.67890; char c[100]; cout.fill('*'); cout.flags(ios_base::left); cout.width(12); cout<

流操作子实现

#include #include using namespace std; int main(){ long a=234;

double b=2345.67890; char c[100]; cout<4.2编写一程序,将两个文件合并成一个文件。 #include #include using namespace std; int main(){ int i=1; char c[1000]; ifstream ifile1(\ ifstream ifile2(\ ofstream ofile(\ while(!ifile1.eof()){ ifile1.getline(c,999); ofile<

4.3编写一程序,统计一篇英文文章中单词的个数与行数。 #include #include using namespace std; bool isalph(char); int main(){ ifstream ifile(\ char text[1000]; bool inword=false; int rows=0,words=0; int i; while(!ifile.eof()){

ifile.getline(text,999); rows++; i=0; while(text[i]!=0){ if(!isalph(text[i])) inword=false; else if(isalph(text[i]) && inword==false){ words++; inword=true; } i++; } } cout<<\ cout<<\ ifile.close (); return 0; }

bool isalph(char c){ return ((c>='A' && c<='Z') || (c>='a' && c<='z')); }

4.4编写一程序,将C++源程序每行前加上行号与一个空格。 #include #include using namespace std; int main(){ int i=1; char c[1000]; ifstream ifile(\ ofstream ofile(\ while(!ifile.eof()){ ofile<

4.5编写一程序,输出 ASCII码值从20到127的ASCII码字符表,格式为每行10个。 #include using namespace std; int main(){

int i,l; for(i=32;i<127;i++){ cout<

4.6将第8章习题7.6 ClockwithDate类对象以”年-月-日 时:分:秒”格式,以文本形式输出之文件,并对插入操作符<<进行重载。

ostream& operator<<(ostream& out,ClockwithDate& CD){ out<

4.7定义一个Student类,其中含学号、姓名、成绩数据成员。建立若干个Student类对象,将它们保存到文件Record.dat中。然后显示文件中的内容。 #include #include #include //using namespace std; class Student{ char Id[10]; char Name[10]; int Score; public: Student(); Student(char *id,char *name,int score); Student(Student &s); ~Student(); friend ostream& operator<<(ostream&,Student&); };

Student::Student(){ strcpy(Id,\ strcpy(Name,\ Score=-1; }

Student::Student(char *id,char *name,int score){ strcpy(Id,id); strcpy(Name,name); Score=score; }

Student::Student(Student &s){ strcpy(Id,s.Id); strcpy(Name,s.Name); Score=s.Score; }

Student::~Student(){}

ostream& operator<<(ostream& out,Student& stu){ out<

int main(){ int i; Student stu[5]={Student(\ Student(\00)}; Student s1[5]; ofstream ostu; ifstream istu; ostu.open(\ for(i=0;i<5;i++) ostu.write((char*)&stu[i],sizeof(Student)); ostu.close(); istu.open(\ for(i=0;i<5;i++){ istu.read((char*)&s1[i],sizeof(Student)); cout<

4.8将学校里的学生定义为一个学生数组类,数组对象动态建立,初始为3个元素,不够用时扩充一倍。要求放在构造函数中用二进制数据文件建立数组元素对象,在析构函数中保存数据和关闭文件。第1次运行时,建立空的数据文件,由键盘输入建立数组元素对象,并写入文件,程序退出时关闭文件,下一次运行时就可以由文件构造对象,恢复前一次做过的工作。

本文来源:https://www.bwwdw.com/article/0kvd.html

Top