This document discusses C++ streams and stream classes. It explains that streams represent the flow of data in C++ programs and are controlled using classes. The key classes are istream for input, ostream for output, and fstream for file input/output. It provides examples of reading from and writing to files using fstream, and describes various stream manipulators like endl. The document also discusses the filebuf and streambuf base classes that perform low-level input/output operations.
2. What Is C++?
▪ C++, as the name suggests, is a superset of C.
▪ C++ can run most of C code while C cannot run C++ code.
▪ C follows the Procedural Programming Language(POP)
▪ while C++ is a language(procedural as well as object oriented)
▪ In C the data is not secured while in C++ data is secured(hidden) .
▪ Encapsulation OF Data Is Done In C++
▪ In C Importance is given on doingThings(Algorithm) While In C++
importance are given on Data (Object).
▪ C uses the top-down approach while C++ uses the bottom-up
approach.
3. What Is Stream?
What is its needs in c++?
A stream is nothing but a flow of data.
In the object-oriented programming, the streams are controlled using
the classes.The operations with the files mainly consist of two types.
They are read and write.
C++ provides various classes, to perform these operations.
The ios class is the base class. All other classes are derived from the ios
class.These classes contain several member functions that perform
input and output operations.
4. Different streams are used to represent
different kinds of data flow.
Each stream is associated with a particular
class, which contains member functions and
definitions for dealing with that particular kind
of data flow.
The stream that supplies data to the program in
known as an input stream. It reads the data
from the file and hands it over to the program.
The stream that receives data from the program
is known as an output stream. It writes the
received data to the file
The following Figure illustratesThis.
5. The Istream and ostream classes
control input and output functions,
respectively.
The ios is the base class of these two
classes.
The member
functions of these classes handle
formatted and unformatted operations.
ios
Istream
Used for INPUT
function
Ostream
Used for Output
Function
7. Header file Brief description
<iostream>
Provide basic information required for all stream I/O operation such as cin, cout, cerr and clog
correspond to standard input stream, standard output stream, and standard unbuffered and
buffered error streams respectively.
<iomanip>
Contains information useful for performing formatted I/O with parameterized stream manipulation.
<fstream> Contains information for user controlled file processing operations.
<strstream>
Contains information for performing in-memory formatting or in-core formatting. This resembles
file processing, but the I/O operation is performed to and from character arrays rather than files.
<stdiostrem>
Contains information for program that mixes the C and C++ styles of I/O.
iostream Library
8. DataType Description
ofstream
This data type represents the output file
stream and is used to create files and to write
information to files.
ifstream
This data type represents the input file stream
and is used to read information from files.
fstream
This data type represents the file stream
generally, and has the capabilities of both
ofstream and ifstream which means it can
create files, write information to files, and
read information from files.
To perform file processing in c++, header files <iostream> and <fstream> must be included in your c++ source
file.
DataTypes In Ostream class
This data types requires another standard C++ library called fstream, which defines three new data types:
9. Mode Flag Description
ios::app
Append mode. All output to that file to be
appended to the end.
ios::ate
Open a file for output and move the
read/write control to the end of the file.
ios::in Open a file for reading.
ios::out Open a file for writing.
ios::trunc
If the file already exists, its contents will be
truncated before opening the file.
Mode Flag Of iostream
Opening a File:
A file must be opened before you can read from it or write to it. Either the ofstream or
fstream object may be used to open a file for writing
And
ifstream object is used to open a file for reading purpose only.
void open(const char *filename, ios::openmode mode);
10. Closing a File
When a C++ program terminates it automatically closes flushes all the streams, release all the allocated memory
and close all the opened files. But it is always a good practice that a programmer should close all the opened files
before program termination.
Following is the standard syntax for close() function, which is a member of fstream, ifstream, and ofstream objects.
void close();
11. Operators Brief description
cin Object of istream class, connected to the standard input device, normally the keyboard.
cout
Object of ostream class, connected to standard output device, normally the
display/screen.
cerr
Object of the ostream class connected to standard error device. This is unbuffered output,
so each insertion to cerr causes its output to appear immediately.
clog Same as cerr but outputs to clog are buffered.
Writing to a File:
The left shift operator (<<) is overloaded to designate stream output and is called stream insertion operator.
write information to a file from your program using the
stream insertion operator (<<) just as you use that operator to output information to the
screen.The only difference is that you use an of stream or fstream object instead of the cout object.
Reading from a File:
The right shift operator (>>) is overloaded to designate stream input and is called stream extraction
operator.
You read information from a file into your program using the stream extraction operator (>>)
just as you use that operator to input information from the keyboard.The only difference is that you use an
Ifstream or Fstream object instead of the Cin object.
12. Read &Write Example:-
// string output using <<
#include <iostream>
void main()
{
cout<<"Welcome to C++ I/O module!!!"<<endl;
cout<<"Welcome to ";
cout<<"C++ module 18"<<endl;
// endl is end line stream manipulator
}
Stream output program example:-
Output:
13. Stream input program example:-
#include <iostream.h>
void main()
{
int p, q, r;
cout << "Enter 3 integers separated by space: n";
cin>>p>>q>>r; // the >> operator skips whitespace characters
cout<<"Sum of the "<<p<<","<<q<<" and "<<r<<" is = "<<(p+q+r)<<endl;
} Output:
14. From File Read &Write Example using fstream class
#include <fstream>
#include <iostream>
using namespace std;
int main ()
{
char data[100];
ofstream outfile;
outfile.open("afile.dat"); // open a file in write mode.
cout << "Writing to the file" << endl;
cout << "Enter your name: ";
cin.getline(data, 100);
outfile << data << endl;
cout << "Enter your age: "; // write inputted data into the file.
cin >> data;
cin.ignore();
outfile << data << endl; // again write inputted data into the file.
outfile.close(); // close the opened file.
15. ifstream infile; // open a file in read mode.
infile.open("afile.dat");
cout << "Reading from the file" << endl;
infile >> data;
cout << data << endl; // write the data at the screen.
infile >> data; // again read the data from the file and display it.
cout << data << endl;
infile.close(); // close the opened file.
return 0;
}
16. Writing to the file
Enter your name: XYZ..
Enter your age: 9
Reading from the file
XYZ..
9
Output
17. filebuf accomplishes input and output
operations with files. The streambuf class
does not organize streams for input or
output operations.
The derived classes of streambuf perform
these operations. It also arranges a
spacefor keeping input data and for sending
output.
The filebuf class is a derived class
of streambuf that is specialized for buffered
disk file I/O.The buffering is managed entirely
within the iostream Class Library.
filebufmember functions call the run-time
low-level I/O routines (the functions declared
in <iostream.h>
Filebuf class in iostream
18. The I/O functions of the classes
istream
and
ostream
invoke the filebuf functions to perform the insertion or
extraction on the streams
The fstreambase acts as a base class for fstream, ifstream,and ofstream.
It holds constant openprototype used in function open() and close() as a member.
19. The fstream:
It allows both simultaneous input and output operations on a filebuf.
The member function of the base classes istream and ostream starts the input and output.
The ofstream:
This class is derived from fstreambase and ostream classes. It can access the member
functions such as
put() , write() ,display() and It allows output operations and provides the member function
with thedefault output mode.
The ifstream:
This class is derived from fstreambase and istream by multiple inheritance. It can access
the member functions such as get(), getline() , and read()
It allows input operations function with the default input mode.
20. Program of class student having data member having rollno,name,address.
Accept And Display data for one object
#include<iostream.h> //stream header file and its library is used
#include<conio.h>
class student //class as student is declared.
{
Int roll; variables initialized
Char name[20];
Public: //Access specified as public.
Void accept() // “istream “ is used to accept data from user.
{
Cout<<“n Enter the Name Of student:-”; // cout operator is used to display on screen
Cin>>name; // cin operator is used to stored vale on disk,
Cout<<“n Enter the roll no of student:-”;
Cin >>roll;
}
Void display()
{
Cout<<“n Name of Student :-”<< name; // cout operator is used to display on screen
Cout<<“n Roll No Of Student:-”<<roll; // cout operator is used to display on screen
}
“<<“ & “>>” operator are used for
insertion and extraction of data from
file .
Bottom
Up
Approach
21. Void display()
{
Cout<<“n Name of Student :-”<< name; // cout operator is used to display on screen
Cout<<“n Roll No Of Student:-”<<roll; // cout operator is used to display on screen
}
};
Void main()
{
Student s1
S1.accept();
S1.display();
Getch();
}
Bottom
Up
Approach
Output
EnterThe Name Of Student:- PPT
EnterThe Roll No:- 001
Name Of The Student Is:- PPT
Roll No :-001