Slide 6- 5
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
I/O Streams
I/O refers to program input and output
Input is delivered to your program via a stream object
Input can be from
The keyboard
A file
Output is delivered to the output device via a stream
object
Output can be to
The screen
A file
Slide 6- 6
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Objects
Objects are special variables that
Have their own special-purpose functions
Set C++ apart from earlier programming
languages
Slide 6- 7
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Streams and Basic File I/O
Files for I/O are the same type of files used to
store programs
A stream is a flow of data.
Input stream: Data flows into the program
If input stream flows from keyboard, the program will
accept data from the keyboard
If input stream flows from a file, the program will accept
data from the file
Output stream: Data flows out of the program
To the screen
To a file
Slide 6- 8
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
cin And cout Streams
cin
Input stream connected to the keyboard
cout
Output stream connected to the screen
cin and cout defined in the iostream library
Use include directive: #include <iostream>
You can declare your own streams to use with
files.
Slide 6- 9
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Why Use Files?
Files allow you to store data permanently!
Data output to a file lasts after the program ends
An input file can be used over and over
No typing of data again and again for testing
Create a data file or read an output file at your
convenience
Files allow you to deal with larger data sets
Slide 6- 10
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
File I/O
Reading from a file
Taking input from a file
Done from beginning to the end (for now)
No backing up to read something again (OK to start over)
Just as done from the keyboard
Writing to a file
Sending output to a file
Done from beginning to end (for now)
No backing up to write something again( OK to start over)
Just as done to the screen
Slide 6- 11
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Stream Variables
Like other variables, a stream variable…
Must be declared before it can be used
Must be initialized before it contains valid data
Initializing a stream means connecting it to a file
The value of the stream variable can be thought of
as the file it is connected to
Can have its value changed
Changing a stream value means disconnecting from
one file and connecting to another
Slide 6- 12
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Streams and Assignment
A stream is a special kind of variable called
an object
Objects can use special functions to complete tasks
Streams use special functions instead of the
assignment operator to change values
Slide 6- 13
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Declaring An
Input-file Stream Variable
Input-file streams are of type ifstream
Type ifstream is defined in the fstream library
You must use the include and using directives
#include <fstream>
using namespace std;
Declare an input-file stream variable using
ifstream in_stream;
Slide 6- 14
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Declaring An
Output-file Stream Variable
Ouput-file streams of are type ofstream
Type ofstream is defined in the fstream library
You must use these include and using directives
#include <fstream>
using namespace std;
Declare an input-file stream variable using
ofstream out_stream;
Không có nhận xét nào:
Đăng nhận xét