lecture%201 - Lecture 01 Comp201 Chapter 6 I/O Streams as...

Info iconThis preview shows pages 1–14. Sign up to view the full content.

View Full Document Right Arrow Icon
Lecture 01 Comp201
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Chapter 6 I/O Streams as an Introduction to Objects and Classes
Background image of page 2
Overview 6.1 Streams and Basic File I/O 6.2 Tools for Stream I/O 6.3 Character I/O 6.4 Inheritance
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
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
Background image of page 4
Objects Objects are special variables that Have their own special-purpose functions Set C++ apart from earlier programming languages
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
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
Background image of page 6
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.
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
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
Background image of page 8
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
Background image of page 9

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
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
Background image of page 10
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
Background image of page 11

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
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;
Background image of page 12
Declaring An Output-file Stream Variable Ouput-file streams of are type ofstream
Background image of page 13

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 14
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 04/17/2008 for the course COMP SCI 201 taught by Professor Durga during the Spring '08 term at Wentworth Institute of Technology.

Page1 / 55

lecture%201 - Lecture 01 Comp201 Chapter 6 I/O Streams as...

This preview shows document pages 1 - 14. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online