1 / 27

输入和输出

第六章. 输入和输出. 回顾. 面向对象程序设计思想 需求分析. 目标. C++ 输入输出的基本概念 输入和输出的相关头文件和类 cout 对象 cin 对象 格式化输入输出. 标准输入输出. 数据的输入和输出( input/output 简写为 I/O ) 对标准输入设备和标准输出设备的输入输出简称为标准 I/O 对在外存磁盘上文件的输入输出简称为文件 I/O 对内存中指定的字符串存储空间的输入输出简称为串 I/O. C++ 流的概念. C++ 中把数据之间的传输操作称作流 输出流:可以表示数据从内存传送到某个载体或设备中

uriel
Download Presentation

输入和输出

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. 第六章 输入和输出

  2. 回顾 • 面向对象程序设计思想 • 需求分析

  3. 目标 • C++ 输入输出的基本概念 • 输入和输出的相关头文件和类 • cout对象 • cin对象 • 格式化输入输出

  4. 标准输入输出 • 数据的输入和输出(input/output简写为I/O) • 对标准输入设备和标准输出设备的输入输出简称为标准I/O • 对在外存磁盘上文件的输入输出简称为文件I/O • 对内存中指定的字符串存储空间的输入输出简称为串I/O

  5. C++流的概念 • C++中把数据之间的传输操作称作流 • 输出流:可以表示数据从内存传送到某个载体或设备中 • 输入流:可以表示数据从某个载体或设备传送到内存缓冲区变量中

  6. 输入和输出的类 • 输入和输出包括的类主要有ios,istream,ostream,iostream,ifstream,ofstream,fstream,istrstream,ostrstream,strstream等。 • ios为根基类,它直接派生四个类:输入流类istream、输出流类ostream、文件流基类fstream和字符串流基类strstream。

  7. 头文件 • #inlcude <iostream.h>这是一个预处理命令 • 当需要进行标准I/O操作时,则必须包含头文件iostream.h • iostream.h系统头文件包含有:ios,iostream, istream, ostream等。

  8. 四个输入输出对象 • C++为用户进行标准I/O操作定义了四个类对象: cin,cout,cerr和clog • cin为istream流类的对象,代表标准输入设备键盘,后三个为ostream流类的对象 • cout代表标准输出设备显示器 • cerr和clog含义相同,均代表错误信息输出设备显示器

  9. 重载运算符<< • C++的流通过重载运算符“<<”执行输出操作。输出操作是向流中插入一个字符序列,因此,在流操作中,将运算符“<<”称为插入运算符。 • 函数原形: ostream& operator<<(简单类型标识符); • 简单类型标识符可以为char,signed char,int, unsigned char,short,unsigned short,long, unsigned int,unsigned long,float,double, long double,char*,signed char*,void * unsigned char*之中的任何一种

  10. cout输出示例 #include <iostream.h> void main() { char sex='M'; int age=40; char name[21]="T-BAG"; cout<<"Name:"<<name<<endl; cout<<"Sex:"<<sex<<endl; cout<<"Age:"<<age<<endl; }

  11. 重载运算符>> • C++的流通过重载运算符“>>”执行输入操作。输入操作是从流中提取一个字符序列,因此,将运算符“>>”称为提取运算符。 • 函数原型: istream& operator>>(简单类型标识符&); • 简单类型标识符除了与在ostream流类中声明<<操作符重载函数给出的所有简单类型标识符基本相同以外,少一个void* 类型

  12. cin输出示例 #include <iostream.h> void main() { char sex='M'; int age=0; float pay=0.0; char name[21]=""; cout<<"Input Name:"; cin>>name; cout<<"Input Sex[M OR F]:"; cin>>sex; cout<<"Input Age And Pay:"; cin>>age>>pay; }

  13. 格式化的输入输出 • 数据输入输出的格式控制使用系统头文件iomanip.h中提供的操纵符。把它们作为插入操作符<<(个别作为提取操作符>>)的输出对象即可。如setiosflags、setw、setfill、setprecision、hex、oct等 • 通过调用流的成员函数控制格式,如setf、unsetf、width、fill、precision等。优点是在设置格式同时,可以返回以前的设置,便于恢复原来的设置。

  14. 常用的流操纵算子

  15. 使用流操纵算子 #include <iostream.h> #include <iomanip.h> #include <math.h> void main(){ int n; double log2 = log( 2.0 ); cout << "Enter a decimal number: "; cin >> n; cout << n << " in hexadecimal is: " << hex << n << endl << dec << n << " in octal is: " << oct << n << endl; cout << "log(2) with precisions 0-2.\n"<< "log(2)= "<<log2 << endl; cout<<setw(15)<<setprecision(0)<<log2<<'\n'; cout<<setw(15)<<setprecision(1)<<log2<<'\n'; cout<<setw(15)<<setprecision(2)<<log2<<'\n'; }

  16. ios类中的枚举常量 • 在根基类ios中定义一个无名枚举类型,其中定义的每个枚举常量都是用于设置控制输入输出格式的标志使用的。 • 该枚举类型定义如下: enum {skipws,left,right,internal,dec,oct,hex,showbase, showpoint, uppercase,showpos,scientific,fixed, unitbuf,stdio}; • 使用时在每个枚举类型常量前加上ios:: cout<<setw(30)<<setiosflags(ios::left)<<“Hello”;

  17. 枚举常量的含义

  18. ios类的成员函数3-1 • ios类提供成员函数对流的状态进行检测和进行输入输出格式控制等操作

  19. ios类的成员函数3-2

  20. ios类的成员函数3-3 #include<iostream.h> void main(){ int x=123; double y=-3.456789; //设置输出下一个数据的域宽为10,默认右对齐,默认填充空格 cout<<"x="; cout.width(10); cout<<x; cout<<"y="; cout.width(10); cout<<y<<endl; //设置输出下一个数据的域宽为10,设置按左对齐输出 cout.setf(ios::left); cout<<"x="; cout.width(10); cout<<x; cout<<"y="; cout.width(10); cout<<y<<endl; cout.fill('*'); //设置填充字符为'*' cout.precision(3); //设置浮点数输出精度为3 cout.setf(ios::showpos); //设置正数的正号输出 cout<<"x="; cout.width(10); cout<<x; cout<<"y="; cout.width(10); cout<<y<<endl; }

  21. istream的字符的输入函数 • int istream::get() • 函数功能:从输入流中接受一个字符,并返回 • 参数:无 • 返回值:字符的ASCII • istream& istream::get(char &c) • 函数功能:从输入流中接受一个字符,存储在参数中 • 参数:用于接受字符的变量(引用) • 返回值:返回一个对*this(调用对象)的引用

  22. get()函数的使用 #include <iostream.h> void main() { char menu; cout<<“Enter Menu[0-7]:”; /*get函数弥补了提取运算符不能提取空白字符的缺点,它能把任意字符包括空白符提取出来*/ menu=cin.get(); //cin.get(menu); }

  23. ostream的字符的输出函数 • 函数原型:ostream& ostream::put(char c) • 功能:向输出流中插入一个字符(输出一个字符) • 参数:需要输出的字符 • 返回值:返回一个对*this的引用 • 注意:使用put函数输出数据不受格式影响,即设置的域宽和填充字符对于put函数不起作用。

  24. put()函数的使用 #include <iostream.h> void main() { char ch=‘H’; cout.put(ch); cout.put(101); cout.put(‘l’); cout.put(‘l’); cout.put(‘o’); cout.put(‘\n’); }

  25. 字符串流 • 为了能让字符数组作为设备并提供输入输出操作,C++引入了ostrstream、istrstream、 strstream这三个类,要使用他们创建对象就必须包含strstream.h头文件。 • istrstream类用于执行C风格的串流的输入操作,也就是以字符串数组作为输入设备。 • ostrstream类用于执行C风格的串流的输出操作,也就是一字符串数组作为输出设备。 • strstream类同时可以支持C风格的串流的输入输出操作。

  26. 字符串流的使用 #include <iostream> #include <strstream> using namespace std; int main(){ char str[100]="Hello World"; istrstream input(str); char ch; input.get(ch); cout<<"ch="<<ch<<endl; return 0; }

  27. 总结 • C++ 流的基本概念 • 输入和输出的相关头文件和类 • cout对象 • cin对象 • ios类的成员函数 • 使用流操纵算子格式化输入输出

More Related