中国高校课件下载中心 》 教学资源 》 大学文库

《C++面向对象程序设计》课程教学资源(PPT课件)Chapter 12 Streams and File IO

文档信息
资源类别:文库
文档格式:PPT
文档页数:44
文件大小:677KB
团购合买:点击进入团购
内容简介
《C++面向对象程序设计》课程教学资源(PPT课件)Chapter 12 Streams and File IO
刷新页面文档预览

Chapter 12 ABSOLUTE C++ Streams and File I/O WALTER SAVITCH SECOND EDITION PEARSON Copyright2006 Pearson Addison-Wesley All rights reserved

Chapter 12 Streams and File I/O

Learning Objectives ◆l/O Streams ◆File I/O ◆Character I/O ◆Tools for Stream I/O ◆File names as input Formatting output,flag settings ◆Stream Hierarchies Preview of inheritance Random Access to Files Copyright 2006 Pearson Addison-Wesley.All rights reserved. 12-2

Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 12-2 Learning Objectives ¨ I/O Streams ¨ File I/O ¨ Character I/O ¨ Tools for Stream I/O ¨ File names as input ¨ Formatting output, flag settings ¨ Stream Hierarchies ¨ Preview of inheritance ¨ Random Access to Files

Introduction ◆Streams ◆Special objects Deliver program input and output ◆File 1/o ◆Uses inheritance Not covered until chapter 14 +File 1/O very useful,so covered here Copyright006 Pearson Addison-Wesley.All rights reserved. 12-3

Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 12-3 Introduction ¨ Streams ¨Special objects ¨Deliver program input and output ¨ File I/O ¨Uses inheritance ¨Not covered until chapter 14 ¨File I/O very useful, so covered here

Streams ◆A flow of characters ◆Input stream ◆Flow into program Can come from keyboard ◆Can come from file ◆Output stream ◆Flow out of program ◆Can go to screen ◆Can go to file Copyright 2006 Pearson Addison-Wesley.All rights reserved. 12-4

Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 12-4 Streams ¨ A flow of characters ¨ Input stream ¨ Flow into program ¨ Can come from keyboard ¨ Can come from file ¨ Output stream ¨ Flow out of program ¨ Can go to screen ¨ Can go to file

Streams Usage We've used streams already ◆cin +Input stream object connected to keyboard ◆Cout Output stream object connected to screen Can define other streams ◆To or from files Used similarly as cin,cout Copyright006 Pearson Addison-Wesley.All rights reserved. 12-5

Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 12-5 Streams Usage ¨ We’ve used streams already ¨cin ¨Input stream object connected to keyboard ¨cout ¨Output stream object connected to screen ¨ Can define other streams ¨To or from files ¨Used similarly as cin, cout

Streams Usage Like cin,cout ◆Consider: Given program defines stream inStream that comes from some file: int theNumber; inStream >theNumber; Reads value from stream,assigned to theNumber Program defines stream outStream that goes to some file outStream <"theNumber is"<<theNumber; Writes value to stream,which goes to file Copyright 2006 Pearson Addison-Wesley.All rights reserved. 12-6

Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 12-6 Streams Usage Like cin, cout ¨ Consider: ¨ Given program defines stream inStream that comes from some file: int theNumber; inStream >> theNumber; ¨ Reads value from stream, assigned to theNumber ¨ Program defines stream outStream that goes to some file outStream << "theNumber is " << theNumber; ¨ Writes value to stream, which goes to file

Files ◆We''ll use text files ◆Reading from file When program takes input ◆Writing to file When program sends output Start at beginning of file to end Other methods available We'll discuss this simple text file access here Copyright006 Pearson Addison-Wesley.All rights reserved. 12-7

Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 12-7 Files ¨ We’ll use text files ¨ Reading from file ¨ When program takes input ¨ Writing to file ¨ When program sends output ¨ Start at beginning of file to end ¨ Other methods available ¨ We’ll discuss this simple text file access here

File Connection Must first connect file to stream object ◆For input: ◆File→ifstream object ◆For output: ◆File→ofstream object Classes ifstream and ofstream Defined in library Named in std namespace Copyright 2006 Pearson Addison-Wesley.All rights reserved. 12-8

Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 12-8 File Connection ¨ Must first connect file to stream object ¨ For input: ¨ File  ifstream object ¨ For output: ¨ File  ofstream object ¨ Classes ifstream and ofstream ¨ Defined in library ¨ Named in std namespace

File 1/O Libraries To allow both file input and output in your program: #include using namespace std; OR #include using std:ifstream; using std:ofstream; Copyright006 Pearson Addison-Wesley.All rights reserved. 12-9

Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 12-9 File I/O Libraries ¨ To allow both file input and output in your program: #include using namespace std; OR #include using std::ifstream; using std::ofstream;

Declaring Streams Stream must be declared like any other class variable: ifstream inStream; ofstream outStream; Must then "connect"to file: inStream.open("infile.txt"); Called "opening the file" Uses member function open Can specify complete pathname Copyright 2006 Pearson Addison-Wesley.All rights reserved. 12-10

Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 12-10 Declaring Streams ¨ Stream must be declared like any other class variable: ifstream inStream; ofstream outStream; ¨ Must then "connect" to file: inStream.open("infile.txt"); ¨Called "opening the file" ¨Uses member function open ¨Can specify complete pathname

刷新页面下载完整文档
VIP每日下载上限内不扣除下载券和下载次数;
按次数下载不扣除下载券;
注册用户24小时内重复下载只扣除一次;
顺序:VIP每日次数-->可用次数-->下载券;
相关文档