294-14-计算机程序计-1标准库和输入 输出流(赵老师).ppt_第1页
294-14-计算机程序计-1标准库和输入 输出流(赵老师).ppt_第2页
294-14-计算机程序计-1标准库和输入 输出流(赵老师).ppt_第3页
294-14-计算机程序计-1标准库和输入 输出流(赵老师).ppt_第4页
294-14-计算机程序计-1标准库和输入 输出流(赵老师).ppt_第5页
已阅读5页,还剩36页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1 计算机程序设计 第14章标准库和输入输出流 西安交通大学计算机教学实验中心 2 本章内容 标准库概述流概述输入输出流格式化I O文件处理 3 14 1标准库概述 所谓标准库指用C 语言编写的函数库和类库由编译器厂商提供与平台 厂商和编译器版本无关库中的函数和类都是最基本 最常用的建立标准库的目的是 减少程序员开发程序的工作量提高程序质量提高编程效率 4 标准库构成 标准函数库从C语言中继承下来例如字符与字符串处理函数 数学函数 时间日期函数 动态分配函数及其他实用函数标准类库标准C 的I O流类 字符串类 数字类 异常处理和杂项类以及STL容器类标准常量和变量 5 标准库的使用 包含相关函数和类的头文件 include或 include math h C 的头文件来源或种类 标准C语言库函数的头文件 带有 h后缀 例如 include math h和iostream h及istream h标准C 语言类库的头文件 不带 h后缀 例如 include iostream和string等由标准C语言库函数头文件变成的标准C 的头文件 把原有标准C语言库函数头文件去掉 h后缀而加上c前缀 例如 include cmath和cstring等名字空间 usingnamespacestd 6 14 2流的概念 流 是以字节方式对数据从源到目标进行传递的一种描述 输入流 指从输入设备到内存中字节的传递 输出流 指从内存到输出设备中字节的传递 C 中有两组类提供两个级别的I O功能 基本的低级I O 是无格式的I O操作 使用不方便 从ios类派生 高级的I O操作 把若干字节组成有意义的数据单位 按数据类型敏感的方式执行 ios类还提供对文件处理的流类 称为文件流 7 14 3输入 输出流 1 输入输出流的头文件基本输入输出 iostream文件流 fstream格式化流 iomanip2 输入输出类对象cin 标准输入流对象cout 标准输出流对象cerr 非缓冲标准错误流clog 经缓冲标准错误流 为流提取运算符 重载右移位运算符 8 I O流类中的其它成员函数 1 get istream类 可读取输入的空格字符intget 读取一个字符如 ch cin get istream 9 14 1 includeusingnamespacestd intmain constintSIZE 80 charch1 ch2 ch3 buffer1 SIZE buffer2 SIZE cout buffer1 cout Thestringreadwithcinis n buffer1 ch1 cin get ch2 cin get cout nTheresultofcin get is n ch1 ch2 cin get ch3 cout nTheresultofcin get ch3 is n ch3 cin get buffer2 SIZE cout nThestringreadwithcin get buffer2 SIZE is n buffer2 endl return0 10 例14 1cinandcin get 11 其他函数 3 put ostream类 输出一个字符如 cout put A 与cout put 65 等价4 ignore int istream类 跳过指定数量的字符5 putback char istream类 将get 提取的字符放回到输入流中 6 peek istream类 返回输入流中的下一个字符 并不删除 7 read write 定长度输入 输出字符到数组中8 gcount 统计最后输入的字符个数 12 14 4格式化I O控制 格式化输入 输出 按规定格式输入输出设置域的宽度 指定数制基数 指定小数位数格式化I O有两种方式 调用ios类的成员函数 使用流操作符 操纵算子manipulator 13 1 调用ios类的成员函数 cout cin 的成员函数setf 设置一个标志位unsetf 终止设置的格式标志width 设置宽度precision 对于浮点数设置精度fill 设置填充字符flags 返回流格式的当前设置或设置标志 14 ios类流格式状态标志位 设置多个状态标志 ios left ios fixed 15 例 利用ios类的成员函数进行格式控制 流类成员函数格式控制 include includeusingnamespacestd intmain doublea 10 1 b 1 23456789 cout width 12 cout setf ios right cout a endl cout b endl cout unsetf ios right cout setf ios left cout a endl cout b endl cout width 12 cout fill cout setf ios right ios showpos cout a endl cout width 12 cout fill cout unsetf ios right cout setf ios left cout b endl return0 16 流类成员函数的精度控制 流类成员函数的精度控制演示 includeusingnamespacestd intmain doubleb 12345 1234567890123456789 inti for i 0 i 10 i cout precision i cout b endl cout endl cout setf ios fixed for i 0 i 10 i cout precision i cout b endl return0 17 2 使用流操纵符 流操纵符可以出现在i o表达式中 注意包含头文件iomanip h常用的有 p322 表14 2 set 设置整数的基数 setbase N 或直接用dec hex oct设置域宽 setw intw 设置精度 setprecision intp 设置域填充字符 setfill intch 设置格式化标志 setiosflags 设置清除格式化标志 resetiosflags 在输出流中插入换行符并刷新该流 endl 18 例14 2设置整数流的基数 cin n cout n inhexadecimalis hex n n dec n inoctalis oct n n setbase 10 n indecimalis n endl 19 例14 3设置浮点数中的小数位数 root2 sqrt 2 0 cout setiosflags ios fixed 操纵符函数cout 使用精度操纵符设置 n for places 0 places 5 places cout setprecision places root2 n 20 14 5文件处理 步骤 包含头文件创建I O文件流 对象 打开文件读或写文件关闭文件 includeifstreammyin ofstreammyout myin open 1 txt myout open 2 txt myin a myout a myin close myout close 直接对磁盘文件进行输入或输出操作 21 创建文件流 打开 关闭文件 读写文件 使用文件流类创建文件流对象 如 ifstreamfin 创建输入流finofstreamfout 创建输出流foutfstreamfio 创建输入和输出流fio使用文件流类的成员函数 打开 关闭文件open 文件名 方式 打开文件的函数close 关闭文件的函数从文件输入 文件流对象 变量 变量 输出到文件 文件流对象 表达式 表达式 22 文件打开方式 23 文件操作应用举例例14 4写文件 include include 包含文件流类头文件intmain ofstreamout 定义输出流对象out open AAA 打开文件if out 文件打开失败 out值为假 cout CannotopenAAAfile n return1 out C 89 5 endl out English 93 5 endl out Maths 87 endl out close 关闭文件 例14 4当前在工程中上创建文件名为 AAA的文本文件 将数据输出到文件AAA中 24 例14 5读取文件AAA include 例14 5 includeintmain ifstreamin grade if in cout course grade cout course grade cout course grade cout course grade n in close return0 25 举例 显示一个文件的内容 文件操作 显示文件内容 include includeusingnamespacestd intmain intargc char argv charch if argc 2 return1 ifstreamin argv 1 ios in ios binary 二进制文件ifstreamin AAA ios in ios binary 二进制文件if in cout Cannotopenfile return1 while in 遇到文件结束标志时 in为假 in get ch 包括空格字符if in cout ch in close return0 26 例 从命令行读入一个C 源文件 每一行加上行号后在屏幕上显示出来 文件操作 为文本文件加行号 include include includeusingnamespacestd intmain intargc char argv charch intln 0 统计行号 if argc 2 cerr n return1 ifstreamin argv 1 ios in ios binary 27 ifstreamin tmp cpp ios in ios binary if in cerr Cannotopenfile return1 cout setw 4 ln while in in get ch if in if ch n cout n cout setw 4 ln elsecout ch cout nThereare ln linesinthisfile endl cin get return0 28 为文本文件加行号 解法2 charch 100 intln 0 统计行号ifstreamin tmp cpp ios in ios binary if in cerr Cannotopenfile return1 while in in getline ch 99 if in cout setw 4 ln cout ch endl cout nThereare ln linesinthisfile endl 29 为文件加行号 写入另一个文件 charch 100 intln 0 行数ifstreamin tmp cpp ios in ios binary ofstreamout test1 txt ios out if in cerr Cannotopenfile n return1 if out cerr Cannotopenfileforoutput n return1 while in in getline ch 99 if in out setw 4 ln out ch endl cout nThereare ln linesinthisfile endl in close out close 30 读文件 删除行号 去掉字符串头部空格的函数voidtrimhead char str inti while str 0 i 0 while str i 0 str i str i 1 i 31 删除字符串第1个字符的函数voidtrimfirst char str inti 0 while str i 0 str i str i 1 i 32 charch 100 intln 0 行数ifstreamin test1 txt ios in ios binary if in cerr 0 ch 0 trimfirst ch 删除第1个字符cout ch endl 输出 换行 in close 关闭文件return0 33 二进制文件和文本文件 文件操作 二进制文件与文本文件 include includeusingnamespacestd 主函数intmain inta 10 charstr1 50 hello ofstreamout1 data1 txt ios out ofstreamout2 out2 open data2 txt ios binary if out1 out2 cerr Cannotopenfile n return1 out1 a out1 str1 out2 write char 34 data1data2 35 例 将1 10的阶乘值输出到文件FILE TXT中 36 将1 10的阶乘值输出到文件中 include includeusingnamespacestd 主函数intmain intN 10 inti intfactor ofstreamout factor txt ios out ios app if out cerr Cannotopenfile n return1 factor 1 for i 1 i N i factor factor i 计算阶乘cout i t factor endl 输出到屏幕out i t factor endl 输出到文件 out close 关闭文件cout endl return0 37 本章实验作业 教材第1 2 3 5题补充要求 第2 3题的结果不仅在屏

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论