1 / 21

C++ 面向对象程序设计教程

C++ 面向对象程序设计教程. 主讲教师;贾澎涛. 第七章 C++ 的 I/O 流类库. C++ 系统提供了一个用于输入输出( I/O )操作的类体系,这个类体系提供了对预定义类型进行输入输出操作的能力,程序员也可以利用这个类体系进行自定义类型的输入输出操作。. 7.1C++ 为何建立自己的输入输出系统. 输入输出系统的任务实际上就是以一种稳定的、可靠的方式在设备与内存之间传输数据 由于 C 语言的输入输出系统不支持用户自定义的对象,所以 C++ 系统建立一个可扩展的输入输出系统,它可以通过修改和扩展来加入用户自定义类型及相应操作。. 7.2C++ 的流及流类库.

eytan
Télécharger la présentation

C++ 面向对象程序设计教程

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. C++面向对象程序设计教程 主讲教师;贾澎涛

  2. 第七章 C++的I/O流类库 C++系统提供了一个用于输入输出(I/O)操作的类体系,这个类体系提供了对预定义类型进行输入输出操作的能力,程序员也可以利用这个类体系进行自定义类型的输入输出操作。

  3. 7.1C++为何建立自己的输入输出系统 • 输入输出系统的任务实际上就是以一种稳定的、可靠的方式在设备与内存之间传输数据 • 由于C语言的输入输出系统不支持用户自定义的对象,所以C++系统建立一个可扩展的输入输出系统,它可以通过修改和扩展来加入用户自定义类型及相应操作。

  4. 7.2C++的流及流类库 • 7.2.1C++的流 流实际上就是一个字节序列。输入操作中,字节从输入设备(如键盘、磁盘、网络连接等)流向内存;输出操作中,字节从内存流向输出设备。 • 流具有方向性 • 与输入设备相联系的流称为输入流 • 与输出设备相联系的流称为输出流 • 与输入输出设备相联系的流称为输入输出流 • C++有4个预定义的流对象,它们是: • cin标准输入 cout标准输出 • cerr标准出错信息输出 clog带缓冲的标准出错信息输出

  5. 7.2.2流类库 • C++的流类库使用继承的方法建立起来的一个输入输出流类库,它具有两个平行的基类: • streambuf类 • ios类

  6. 7.3输入输出的格式控制 • 在C++中,仍然可以使用C中的printf()和scanf()函数进行格式化。 • 使用ios类中有关格式控制的成员函数进行格式化 • 使用操纵符(流操纵算子)的特殊类型的函数进行格式化

  7. 7.3.1用ios的成员函数进行格式化 • ios类中有几个成员函数可以用来对输入输出进行格式控制。进行格式控制主要是通过对格式状态字、域宽、填充字符以及输出精度的操作来完成的 • 状态标志字 C++可以给每个流对象的输入输出进行格式控制,以满足用户对输入输出格式的需求。输入输出格式由一个long int类型的状态标志字确定。

  8. skipwsSkip white space on input. • leftLeft-align values; pad on the right with the fill character. • rightRight-align values; pad on the left with the fill character (default alignment). • internalAdd fill characters after any leading sign or base indication, but before the value. • decFormat numeric values as base 10 (decimal) (default radix). • octFormat numeric values as base 8 (octal). • ios::hexFormat numeric values as base 16 (hexadecimal). • ios::showbaseDisplay numeric constants in a format that can be read by the C++ compiler.

  9. showpointShow decimal point and trailing zeros for floating-point values. • uppercaseDisplay uppercase A through F for hexadecimal values and E for scientific values. • showposShow plus signs (+) for positive values. • scientificDisplay floating-point numbers in scientific format. • fixedDisplay floating-point numbers in fixed format. • unitbufCause ostream::osfx to flush the stream after each insertion. By default, cerr is unit buffered. • stdioCause ostream::osfx to flush stdout and stderr after each insertion.

  10. ios类中用于控制输入输出格式的成员函数 • long ios:: setf(long flags); • long ios:: unsetf(long flags); • long ios::flags(); • long ios::flags(long flags); • int ios:: width() • int ios:: width(int w) • int ios::precision(int p); • char ios:: fill(); • char ios:: fill(char ch);

  11. 设置状态标志 • 设置状态标志,即是将某一状态标志位置“1”,可使用setf()函数。 • 例7.1 • 清除状态标志 • 清除状态标志,即是将某一状态标志位置“0”,可使用unsetf(long flags)函数。 • 取状态标志 • 例7.2

  12. 设置域宽 • 设置显示的精度 • 填充字符 • 例7.3、7.4

  13. 7.3.2使用流操纵算子进行输入输出格式控制 • C++预定义的操纵符 • 操纵符的使用 • 例7.5 • 用户自定义操纵符

  14. C++预定义的操纵符 • dec 以十进制形式输入或输出整型数,可用于输入或输出 • hex 以十六进制形式输入或输出整型数,可用于输入或输出 • oct 以八进制形式输入或输出整型数,可用于输入或输出 • ws 用于在输入时跳过开头的空白符,仅用于输入 • endl 插入一个换行符并刷新输入流,仅用于输出 • ends 插入一个空字符,通常用来结束一个字符串,仅用于输出 • flush 刷新一个输出流,仅用于输出

  15. setbase(int n) 把转换基数设置为n(n的取值为0,8,10,或16),n的缺省值为0,即以十进制形式输出 • resetiosflags(long f)关闭由参数f指定的格式标志,可以用于输入或输出 • setiosflags(long f)设置由参数f指定的格式标志,可以用于输入或输出 • setfill(int c)设置c为填充字符,缺省时为空格,可以用于输入或输出 • setprecision(int n)设置数据小数部分的位数 • setw(int n)设置域宽为n,可以用于输入或输出

  16. 操纵符的使用 • 例7.5 • 例7.6

  17. 用户自定义的操纵符 • 若为输出流定义操纵符函数,则定义形式如下: Ostream &manip_name(ostream &stream) { //自定义代码 return stream; } • 若为输入流定义操纵符函数,则定义形式如下: istream &manip_name(istream &stream) { //自定义代码 return stream; }

  18. 7.4用户自定义类型的输入输出 对于用户自定义类型的输入输出,可以通过重载运算符“<<”和“>>”来实现。

  19. 7.4.1重载输出运算符“<<” 重载输出运算符“<<”函数的一般格式如下: ostream &operator<<(ostream &stream,class_name ob) { //操作代码 return stream; } • 例7.9.1 • date

  20. 7.4.2重载输入运算符“>>” 重载输入运算符“>>”函数的一般格式如下: istream &operator>>(istream &stream,class_name &ob) { //操作代码 return stream; } • 例7.11 • 例7.22

More Related