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 3.8将一个字符char c=’A’写到文件中错误的语句为(D)。 A.file.write(reinterpret_cast 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 double b=2345.67890; char c[100]; cout< 4.3编写一程序,统计一篇英文文章中单词的个数与行数。 #include 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 4.5编写一程序,输出 ASCII码值从20到127的ASCII码字符表,格式为每行10个。 #include 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 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次运行时,建立空的数据文件,由键盘输入建立数组元素对象,并写入文件,程序退出时关闭文件,下一次运行时就可以由文件构造对象,恢复前一次做过的工作。
正在阅读:
c++流类库与输入输出习题答案10-01
3大学英语四级考试模拟试题12-31
2022年天津师范大学法学院刑事诉讼法考研复试实战预测五套卷04-05
专转本诗歌鉴赏答题技巧07-21
教学大纲(中文和英文)讲义03-08
国家森林公园旅游基础建设可行性研究报告06-03
oracle两表查询练习附答案03-08
数学复习大纲05-01
- 多层物业服务方案
- (审判实务)习惯法与少数民族地区民间纠纷解决问题(孙 潋)
- 人教版新课标六年级下册语文全册教案
- 词语打卡
- photoshop实习报告
- 钢结构设计原理综合测试2
- 2014年期末练习题
- 高中数学中的逆向思维解题方法探讨
- 名师原创 全国通用2014-2015学年高二寒假作业 政治(一)Word版
- 北航《建筑结构检测鉴定与加固》在线作业三
- XX县卫生监督所工程建设项目可行性研究报告
- 小学四年级观察作文经典评语
- 浅谈110KV变电站电气一次设计-程泉焱(1)
- 安全员考试题库
- 国家电网公司变电运维管理规定(试行)
- 义务教育课程标准稿征求意见提纲
- 教学秘书面试技巧
- 钢结构工程施工组织设计
- 水利工程概论论文
- 09届九年级数学第四次模拟试卷
- c++
- 习题
- 输出
- 输入
- 答案
- 计算机组成原理 2016年上期末试题B
- 管理信息系统判断选择题参考
- 幼儿园小班主题活动方案 (我爱我家)
- 吉大15春学期《路基路面工程》在线作业一满分答案
- 安全就要杜绝习惯性违章
- 数字图像处理技术试题答案
- 上村佳苑基坑开挖方案
- 医院管理学
- 条件状语从句、反义疑问句
- 第八讲 自我管理与职业之三
- 宿根花卉在城市绿地中的应用
- 购买行为案例分析
- 职业性中毒案例讨论(教师用)
- 精选《世说新语》六篇(原文 注释 译文)
- 翻译理论与实践 Lecture 5 Chapter4
- 中国电笔行业市场调查研究报告(目录) - 图文
- 1.1信息与信息技术习题答案
- 浅析新时期大学生宿舍文化建设
- 新疆2016年下半年资产评估师《资产评估》:以财务报告为目的评估报告考试试题
- 特种设备安全培训主要内容