1. Chapter: 12
Stream and Files
Lecture: 47 and 48
Date: 12.11.2012
2. Objectives
Overview of stream classes
Showing how to perform file-related activities using streams:
How to read and write data to files in a variety of ways
How to handle files or I/O errors
How files and OOP are related
3. Streams
Stream (flow of data)
A transfer of information in the form of a sequence of bytes
In C++, a stream is represented by an object of a
particular class; e.g., cin and cout are objects of
iostream class.
I/O Operations:
Input: A stream that flows from an input device ( i.e.: keyboard,
disk drive, network connection) to main memory
Output: A stream that flows from main memory to an output
device ( i.e.: screen, printer, disk drive, network connection)
4. keyboard
standard
input stream
CPU
standard
output MEM
monitor stream
terminal
console
HDD
What does information
travel across?
Streams
5. keyboard
standard
input stream
CPU
standard
output MEM
monitor stream
terminal file
console input
stream
LOAD HDD
What does information READ
travel across? file
files output
Streams stream
SAVE
WRITE
6. C++ Stream Input/output
iostream library has hundreds of I/O capabilities
iostream: basic input and output
fstream: file processing
iostream library contains many I/O related classes:
istream ( the extraction operator >>, and get(), getline(), read() are
members of this class)
ostream (the insertion operator <<, put(), write() are members of
this class)
istream and ostream are subclasses of ios base class
cout is a predefined object of the iostream_withassign
class, whereas cin is a predefined object of
istream_withassign class
7. C++ Stream Input/output
The classes used for input and output to the video
display and keyboard are declared in the header file
iostream, e.g., #include <iostream>
The classes used specifically for disk file I/O are
declared in the fstream header file,
e.g., #include <fstream>
All of them can be found in the include subdirectory
of the C++ compiler.
9. ios Class
The granddaddy of all the stream classes, and
contains the majority of the features needed to
operate C++ streams
Three most important features of ios class are:
1) Formatting flags
2) Error-status flags
3) File operation mode
10. ios Formatting Flags
The formatting flags act as on/off switches that
specify choices for various aspects of input and
output format and operation.
11.
12. ios Formatting Flags
Since they are the members of the ios class, they
must follow the name ios and the scope resolution
operator;
e.g., ios::showpoint
All the flags must be set using the setf() and usetf()
ios member functions;
e.g., cout.setf(ios::showpoint)
cout.unsetf(ios::showpoint)
13. #include <iostream>
#include <conio.h>
using namespace std;
int main(void)
{
float x = 18.0;
cout<< x << endl; //displays 18
cout.setf(ios::showpoint);
cout<< x << endl; //displays 18.0000
cout.setf(ios::scientific);
cout<< x << endl; //displays 1.800000e+001
cout.unsetf(ios::showpoint);
cout.unsetf(ios::scientific);
cout<<x<<endl; //displays 18
getch();
return 0; }
14. ios Formatting Flags
Since they are the members of the ios class, they
must follow the name ios and the scope resolution
operator;
e.g., ios::showpoint
All the flags must be set using the setf() and usetf()
ios member functions;
e.g., cout.setf(ios::showpoint)
cout.unsetf(ios::showpoint)
15. ios Formatting Flags
Many formatting flags can be set using manipulators
Manipulators are formatting instructions inserted
directly into a stream; e.g., endl, oct, setw() etc.
Manipulators come in two flavors:
1) No-argument ios manipulators
2) ios manipulators with arguments
18. #include <iostream>
#include <iomanip>
#include <conio.h>
using namespace std;
int main(void)
{
int var = 11;
cout << setw(8) << 22 << "n";
cout << setw(8) << 4444 << "n";
cout << setw(8) << 666666 << endl;
cout<< var <<“in hexadecimal is ” << hex <<var;
cout<< setpercision(5) <<20.99055;
getch();
return 0; }
19. istream Class
The istream class is derived from ios class and
performs input specific activities.
20.
21. istream Class
cin.get(): inputs a character from stream
(even white spaces) and returns it
cin.get( c ): inputs a character from stream
and stores it in c
22. istream Class
cin.get(array, size):
Accepts 3 arguments: array of characters, the size limit, and a delimiter ( default
of ‘n’).
Uses the array as a buffer
When the delimiter is encountered, it remains in the input stream
Null character is inserted in the array
Unless delimiter flushed from stream, it will stay there
cin.getline(array, size)
Operates like cin.get(buffer, size) but it discards the delimiter from
the stream and does not store it in array
Null character inserted into array
26. Stream Errors
Following statements are well-known to us:
cout<<“Good afternoon”
Or
cin >> var
What if the user enters “five” instead of “5” for
integer variable “var” ?
27. Stream Errors
Following statements are well-known to us:
cout<<“Good afternoon”
Or
cin >> var
What if the user enters “five” instead of “5” for
integer variable “var” ?
The compiler will through an error message!
28. Errors-Status Bits
The stream error status flags report errors that occur
in an input or output operation.