1 / 13

第 9 章 输入输出流

第 9 章 输入输出流. 主要内容包括: 1. I/O 流概述 基本的流操作 C++ 的流类库 2. 输入 / 输出流 输出流 输入流 格式化输出 3. 磁盘文件的输入输出 文件的打开和关闭 文件指针 文本文件的读写 4. 二进制文件的读写. 输入输出流. I/O 流概述 流:数据的流动,从源到目的。 C++ 的流类库 存放与输入输出有关的流类。 基本的流操作 标准输入输出流对象: cin,cout. ios. istream. ostream. iostream. 输入输出流. 输入输出流. 输入 / 输出流

elvis-kirby
Télécharger la présentation

第 9 章 输入输出流

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. 第9章 输入输出流 主要内容包括: 1. I/O流概述 基本的流操作 C++的流类库 2. 输入/输出流 输出流 输入流 格式化输出 3.磁盘文件的输入输出 文件的打开和关闭 文件指针 文本文件的读写 4. 二进制文件的读写

  2. 输入输出流 I/O流概述 流:数据的流动,从源到目的。 • C++的流类库 存放与输入输出有关的流类。 • 基本的流操作 标准输入输出流对象:cin,cout

  3. ios istream ostream iostream 输入输出流

  4. 输入输出流 输入/输出流 ostream类提供了流类库的主要输出操作,istream类提供了流类库的主要输入操作,它们是在头文件iostream.h中定义的。cin,cout是对象。 • 输出流 用于屏幕输出的成员函数 : cout.put(‘a’); cout.write(“I am a student.”,4); • 输入流 用于键盘输入的成员函数 : ch=cin.get(); cin.getline(ch, n); cin.read(ch, n);

  5. 输入输出流 • 格式化输出 int i=5; double j=12.3456789; float a=34.5; cout.width(10); cout.fill(‘*’); cout<<i<<endl; cout.precision(5); cout<<j<<endl; cout.setf(ios::scientific | ios::left | ios::showpos); cout<<a<<endl;

  6. 输入输出流 ios类中定义的标志位有: skipws 跳过输入中的空白 left 左对齐输出数据 right 右对齐输出数据 internal 在数据的符号和数据本身之间插入填充符 dec 以十进制输入/输出整型数据 oct 以八进制输入/输出整型数据 hex 以十六进制输入/输出整型数据 showbase 输出的数值前携带进制符号(显示整数的基数) showpoint 浮点数输出带小数点 uppercase 以大写字母输出十六进制数值 showpos 输出的正数前加“+”号 scientific 以科学计数法输入/输出浮点型数值 fixed 以定点形式表示浮点数 untibuf 清空输出标志,默认是cerr的缓冲单元。

  7. 磁盘文件的输入输出 fstream类用来处理磁盘文件的输入和输出。ifstream类用来处理磁盘文件的输入操作,ofstream类用来处理磁盘文件的输出操作。 定义文件对象: ofstream file1; ifstream file2; fstream file3;

  8. 磁盘文件的输入输出 与磁盘文件有关操作: • 文件的打开和关闭 file1.open(“filename1”); file1.close(); file3.open(“filename3”,ios::out); • 文件指针 文件指针指向文件中下一个要读/写的字节位置 。相关成员函数: • seekp()函数——设置下一个要写的数据位置; • tellp()函数——返回文件当前写的位置,是距文件头的字节数; • seekg()函数——设置下一个将读取数据的位置; • tellg()函数——返回当前文件读的位置,是距文件头的字节数。

  9. 磁盘文件的输入输出 例: file1.seekp(50,ios::beg); 将写指针移到距当前位置50个字节处。 file1.seekp(-50,ios::end); 将写指针移到距文件尾50个字节处。 file1.seekg(-50,ios::cur); 将读指针移到当前位置前50个字节处。 • 文本文件的读写 流类库的输入输出操作<<、put()、write()、>>、get()、getline()等,同样可以用于文件的输入输出。

  10. 磁盘文件的输入输出 例1:编程实现将文字”I am a stuent. I am a college student.”分两行写入磁盘文件”text.dat”。 #include <iostream.h> #include <fstream.h> void main() { fstream file1; file1.open(“text.dat”, ios::out); if(!file1) { cout<<”open error!”<<endl; abort(); } file1<<”I am a student.”<<endl; file1<<”I am a college student.”<<endl; file1.close(); }

  11. 磁盘文件的输入输出 例2:编程实现将例1中text.dat文件中的内容读出并显示在屏幕上。 #include <iostream.h> #include <fstream.h> void main() { fstream file2; file2.open(“text.dat”,ios::in); if(!file2) { cout<<”file open error!”<<endl; abort(); } char s[50]; while(!file2.eof()) { file2.getline(s,sizeof(s)); cout<<s<<endl; } file2.close(); }

  12. 二进制文件的输入输出 二进制文件在用open打开时,指定文件的打开方式ios::binary。向二进制文件写入信息时通常使用write()函数,从二进制文件中读取信息时通常使用read()函数。

  13. C++中异常处理 例3:编程实现例1和例2的功能,要求将文字”I am a stuent. I am a college student.”分两行写入二进制文件”text.dat”中,并从该文件中读出来。 #include <iostream.h> #include <fstream.h> void main() { fstream file; file.open(“text.dat”,ios::in | ios::out | ios::binary); if(!file) { cout<<”file open error!”<<endl; abort(); } file.write(“I am a student.\n”); file.write(“I am a college student\n”); file.seekp(0,ios::beg); //将写指针移到文件头 char ch[50]; while(!file.eof()) { file.read(ch,sizeof(ch)); cout<<ch<<endl; } file.close(); }

More Related