Unit 5 Part1 - OOP
Unit 5 Part1 - OOP
Unit 5 Part1 - OOP
Stream in C++ means a stream of characters that gets transferred between the program thread and input or output.
There are a number of C++ stream classes eligible and defined which is related to the files and streams for providing
input-output operations. All the classes and structures maintaining the file and folders with hierarchies are defined
within the file with iostream.h standard library. Classes associated with the C++ stream include ios class, istream
class, and ostream class. Class ios is indirectly inherited from the base class involving iostream class using istream
class and ostream class which is declared virtually.
C++ Stream Classes with Examples
There is a number of stream classes in the hierarchy which is defining and giving different flows for the varied objects
in the class. The hierarchy is maintained in a way where it gets started from the top class which is the ios class
followed by all the other classes involving istream class, ostream class, iostream class, istream_withassign class, and
ostream_withassign class. The iosclass in the hierarchy is the parent class which is considered as a class from where
both the istream and ostream class gets inherited. Both the istream class and ostream class constitute the ios class
which is the highest level of the entire hierarchy of C++ stream classes. The other classes which include functions for
the operations include assignment operation like _withassign classes.
Various stream classes in C++ are as follows:
Code:
istream class
ostream class
iostream class
ios class
ostream_withassign class
istream_withassign class
1. istream Class
istream being a part of the ios class which is responsible for tackling all the input stream present within the stream. It
provides all the necessary and important functions with the number of functions for handling all the strings, chars, and
objects within the istream class which comprises all these functions such as get, read, put, etc.
Example
This program illustrates the istream class which takes a variable as an input then it makes use of the inbuilt functions
like get to tackle and handle the input stream with the output value as an input being provided to the function as shown
in the output.
#include <iostream>
intmain()
{
char p;
cin.get(p);
cout<< p;
}
2. ostream Class
This class as part of the ios class is also considered as a base class that is responsible for handling output stream and
provides all the necessary functions for handling chars, strings, and objects such as put, write, etc.
Example
This program demonstrates the ostream class as part of the ios class where the first initialized char defined is scanned
and then it gets the scanned character and the ostream function takes care to write or put the value to the function.
#include <iostream>
intmain()
{
char r_t;
cin.get(r_t);
cout.put(r_t);
}
3. iostream Class
iostream class is the next hierarchy for the ios class which is essential for input stream as well as output stream
because istream class and ostream class gets inherited into the main base class. As the name suggests it provides
functionality to tackle the objects, strings, and chars which includes inbuild functions of put, puts, get, etc.
Example
This program is used to demonstrate the iostream class which comprises functions like write to print the input stream
with the required number of values as input as shown in the output.
#include <iostream>
intmain()
{
cout.write("educba_portal", 9);
}
:
4. ios Class
ios class is the highest class in the entire hierarchical structure of the C++ stream. It is also considered as a base class
for istream, stream, and streambuf class. It can be said that the ios class is basically responsible for providing all the
input and output facilities to all the other classes in the stream class of C++.
Example
This program demonstrates the ios class which comprises of the iostream.h as a standard library to derive the values
for input and output stream which is part of the ios class as shown in the output.
# include <iostream>
intmain()
{
cout<<"Get the value for the _io_stream generation";
return 0;
}
5. istream_withassign Class
This class is considered as a variant for the istream class that provides the class privilege for the class to assign object.
The predefined object which can be called a build in the function of this class is used which is responsible for
providing getting the stream facility and thus allows the object to reassign at the run time for different stream objects.
Istream_withassign class acts as the primary class for the other classes as part of the istream class.
Example
This program demonstrates the istream_withassign class which is responsible for creating the object of the class as
shown in the given output.
#include <iostream>
intmain()
{
char istream_withassign[8];
std::cin.get(istream_withassign, 8);
std::cout<< istream_withassign << '\n';
std::cin.get(istream_withassign, 8);
std::cout<< istream_withassign << '\n';
return 0;
}
6. ostream_withassign Class
This class is responsible for providing object assigned to the class and is considered as a variant itself for the ostream
class of the C++ stream. All the build-in functions such as cout, cerr, clog is the already present objects of the same
class and are reassigned at execution time for the different ostream object.
Example
This program demonstrates the ostream_withassign class which is responsible for creating the object of the class as
shown in the given output.
#include <iostream>
intmain()
{
char ostream_withassign[10];
std::cin.get(ostream_withassign, 10);
std::cout<<ostream_withassign<< '\n';
std::cin.get(ostream_withassign, 10);
std::cout<<ostream_withassign<< '\n';
return 0;
}
Conclusion
C++ Stream is a very powerful and versatile functionality of the stream classes. They provide programmers an insight
for using the predefined and in build functions by modification in the object and the standard libraries of the class for
various manipulations and arrangements of the files and folders thus maintaining the hierarchical structure intact for
the C++ stream.
File Handling using File Streams in C++
File represents storage medium for storing data or information. Streams refer to sequence of bytes. In Files we store
data i.e. text or binary data permanently and use these data to read or write in the form of input output operations by
transferring bytes of data. So we use the term File Streams/File handling. We use the header file <fstream>
ofstream: It represents output Stream and this is used for writing in files.
ifstream: It represents input Stream and this is used for reading from files.
fstream: It represents both output Stream and input Stream. So it can read from files and write to files.
Operations in File Handling:
Creating a file: open()
Reading data: read()
Writing new data: write()
Closing a file: close()
Creating/Opening a File
We create/open a file by specifying new path of the file and mode of operation. Operations can be reading, writing,
appending and truncating. Syntax for file creation: FilePointer.open("Path",ios::mode);
int main()
{
fstream st; // Step 1: Creating object of fstream class
st.open("E:\studytonight.txt",ios::out); // Step 2: Creating new file
if(!st) // Step 3: Checking whether file exist
{
cout<<"File creation failed";
}
else
{
cout<<"New file created";
st.close(); // Step 4: Closing file
}
getch();
return 0;
}
Writing to a File
#include <iostream>
#include<conio>
#include <fstream>
int main()
{
fstream st; // Step 1: Creating object of fstream class
st.open("E:\studytonight.txt",ios::out); // Step 2: Creating new file
if(!st) // Step 3: Checking whether file exist
{
cout<<"File creation failed";
}
else
{
cout<<"New file created";
st<<"Hello"; // Step 4: Writing to file
st.close(); // Step 5: Closing file
}
getch();
return 0;
}
Here we are sending output to a file. So, we use ios::out. As given in the program, information
typed inside the quotes after "FilePointer <<" will be passed to output file.
Reading from a File
#include <iostream>
#include<conio>
#include <fstream>
int main()
{
fstream st; // step 1: Creating object of fstream class
st.open("E:\studytonight.txt",ios::in); // Step 2: Creating new file
if(!st) // Step 3: Checking whether file exist
{
cout<<"No such file";
}
else
{
char ch;
while (!st.eof())
{
st >>ch; // Step 4: Reading from file
cout << ch; // Message Read from file
}
st.close(); // Step 5: Closing file
}
getch();
return 0;
}
Here we are reading input from a file. So, we use ios::in. As given in the program, information from
the output file is obtained with the help of following syntax "FilePointer >>variable".
Close a File
It is done by FilePointer.close().
#include <iostream>
#include<conio>
#include <fstream>
int main()
{
fstream st; // Step 1: Creating object of fstream class
st.open("E:\studytonight.txt",ios::out); // Step 2: Creating new file
st.close(); // Step 4: Closing file
getch();
return 0;
}
Copy
int main()
{
fstream st; // Creating object of fstream class
st.open("E:\studytonight.txt",ios::out); // Creating new file
if(!st) // Checking whether file exist
{
cout<<"File creation failed";
}
else
{
cout<<"New file created"<<endl;
st<<"Hello Friends"; //Writing to file
You can detect when the end of the file is reached by using the member function eof() which has
prototype :
int eof();
It returns non-zero when the end of file has been reached, otherwise it returns zero.