0% found this document useful (0 votes)
4 views

OOPlecture8

Uploaded by

Jobayer Ahmed
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

OOPlecture8

Uploaded by

Jobayer Ahmed
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

File Handling in C++ using

classes and STL


CSE-213
Object Oriented language
► File handling is used to store data permanently in a computer. Using file
handling we can store our data in secondary memory (Hard disk).
► How to achieve the File Handling?
► For achieving file handling we need to follow the following steps:-
STEP 1-Naming a file
STEP 2-Opening a file
STEP 3-Writing data into the file
STEP 4-Reading data from the file
STEP 5-Closing a file.
► Streams in C++ :-
► We give input to the executing program and the execution program gives back
the output. The sequence of bytes given as input to the executing program
and the sequence of bytes that comes as output from the executing program
are called stream. In other words, streams are nothing but the flow of data in
a sequence.

► The input and output operation between the executing program and the
devices like keyboard and monitor are known as “console I/O operation”. The
input and output operation between the executing program and files are
known as “disk I/O operation”.
Classes for File stream operations :-

► The I/O system of C++ contains a set of classes which define the file handling
methods. These include
ifstream,
ofstream and
fstream classes.
► These classes are derived from
fstream and from the corresponding iostream class.
► 1. ios:-ios stands for input output stream.
► 2. istream:-istream stands for input stream.This class is derived from the class
‘ios’.
► 3. ostream:-ostream stands for output stream.This class is derived from the
class ‘ios’.
► 4. streambuf:-This class contains a pointer which points to the buffer which is
used to manage the input and output streams.
► 5. fstreambase:-This class provides operations common to the file streams.
Serves as a base for fstream, ifstream and ofstream class.
► 6. ifstream:-This class provides input operations. Inherits the functions get(),
getline(), read(), seekg() and tellg() functions from the istream.
► 7. ofstream:-This class provides output operations. Inherits the functions
put(), write(), seekp() and tellp() functions from the ostream.
► 8. fstream:-This class provides support for simultaneous input and output
operations.
► 9. filebuf:-Its purpose is to set the file buffers to read and write. We can also
use file buffer member function to determine the length of the file.
File Handling
Coding from lab
The C++ Standard Template Library (STL)

► The Standard Template Library (STL) is a set of C++ template classes to


provide common programming data structures and functions such as lists,
stacks, arrays, etc. It is a library of container classes, algorithms, and
iterators. It is a generalized library and so, its components are
parameterized. Working knowledge of template classes is a prerequisite for
working with STL.
Template Classes

► A template is a simple yet very powerful tool in C++. The simple idea is to
pass the data type as a parameter so that we don’t need to write the same
code for different data types
► Two types of templates
► Function template
► Class template
Function template

// C++ Program to demonstrate int main()


{
// Use of template // Call myMax for int
#include <iostream> cout << myMax<int>(3, 7) << endl;
using namespace std; // call myMax for double
cout << myMax<double>(3.0, 7.0) <<
endl;
// One function works for all data // call myMax for char
types. This would work cout << myMax<char>('g', 'e') << endl;
// even for user defined types if
operator '>' is overloaded return 0;
}
template <typename T> T myMax(T x,
T y)
{
return (x > y) ? x : y;
}
Class template
#include <iostream>
using namespace std;
template <typename T> void
template <typename T> class Array { Array<T>::print()
private: {
T* ptr; for (int i = 0; i < size; i++)
int size; cout << " " << *(ptr + i);
cout << endl;
public: }
Array(T arr[], int s);
void print(); int main()
}; {
int arr[5] = { 1, 2, 3, 4, 5 };
template <typename T> Array<T>::Array(T arr[], int s) Array<int> a(arr, 5);
{ a.print();
ptr = new T[s]; return 0;
size = s; }
for (int i = 0; i < size; i++)
ptr[i] = arr[i];
}
Key components of the STL include:

► Containers: The STL provides a range of containers, such as vector, list, map,
set, and stack, which can be used to store and manipulate data.
► Algorithms: The STL provides a range of algorithms, such as sort, find, and
binary_search, which can be used to manipulate data stored in containers.
► Iterators: Iterators are objects that provide a way to traverse the elements of
a container. The STL provides a range of iterators, such as forward_iterator,
bidirectional_iterator, and random_access_iterator, that can be used with
different types of containers.
► Function Objects: Function objects, also known as functors, are objects that
can be used as function arguments to algorithms. They provide a way to pass
a function to an algorithm, allowing you to customize its behavior.
► Adapters: Adapters are components that modify the behavior of other
components in the STL. For example, the reverse_iterator adapter can be
used to reverse the order of elements in a container.
1. Algorithms

► The header algorithm defines a collection of functions specially designed to


be used on a range of elements. They act on containers and provide means for
various operations for the contents of the containers.
► Algorithm
► Sorting
► Searching
► Important STL Algorithms
► Useful Array algorithms
► Partition Operations
► Numeric
► valarray class
2. Containers
► Containers or container classes store objects and data. There are in total
seven standards “first-class” container classes and three container adaptor
classes and only seven header files that provide access to these containers or
container adaptors.
► Sequence Containers: implement data structures that can be accessed in a
sequential manner.
► vector
► list
► deque
► arrays
► forward_list( Introduced in C++11)
► Container Adaptors: provide a different interface for sequential containers.
► queue
► priority_queue
► stack
► Associative Containers: implement sorted data structures that can be quickly
searched (O(log n) complexity).
► set
► multiset
► map
► multimap
► Unordered Associative Containers: implement unordered data structures that
can be quickly searched
3.Functors

► The STL includes classes that overload the function call operator. Instances of
such classes are called function objects or functors. Functors allow the
working of the associated function to be customized with the help of
parameters to be passed.
4. Iterators

► As the name suggests, iterators are used for working on a sequence of values.
They are the major feature that allows generality in STL.

You might also like