SlideShare a Scribd company logo
1 of 27
OOPS & C++(UNIT 5)
BY:SURBHI SAROHA
SYLLABUS
• File Handling
• Exception Handling
• Files I/O
• Exception Handling(Exception Handling Mechanism, Throwing Mechanism, Catching Mechanism, Re-
throwing an Exception)
FILE HANDLING
• 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.
CONT…..
• 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. These classes, designed to manage the disk files, are declared in fstream
and therefore we must include this file in any program that uses files.
1. ios:-
• ios stands for input output stream.
• This class is the base class for other classes in this class hierarchy.
• This class contains the necessary facilities that are used by all the other derived classes for input and
output operations.
CONT…..
• 2. istream:-
• istream stands for input stream.
• This class is derived from the class ‘ios’.
• This class handle input stream.
• The extraction operator(>>) is overloaded in this class to handle input streams from files to the program
execution.
• This class declares input functions such as get(), getline() and read().
CONT…..
• 3. ostream:-
• ostream stands for output stream.
• This class is derived from the class ‘ios’.
• This class handle output stream.
• The insertion operator(<<) is overloaded in this class to handle output streams to files from the program
execution.
• This class declares output functions such as put() and write().
• 4. streambuf:-
• This class contains a pointer which points to the buffer which is used to manage the input and output
streams.
CONT…..
• 5. fstreambase:-
• This class provides operations common to the file streams. Serves as a base for fstream, ifstream and ofstream
class.
• This class contains open() and close() function.
• 6. ifstream:-
• This class provides input operations.
• It contains open() function with default input mode.
• Inherits the functions get(), getline(), read(), seekg() and tellg() functions from the istream.
• 7. ofstream:-
• This class provides output operations.
• It contains open() function with default output mode.
CONT……
• Inherits the functions put(), write(), seekp() and tellp() functions from the ostream.
• 8. fstream:-
• This class provides support for simultaneous input and output operations.
• Inherits all the functions from istream and ostream classes through iostream.
• 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.
CONT…..
• In C++, files are mainly dealt by using three classes fstream, ifstream, ofstream available in fstream
headerfile.
ofstream: Stream class to write on files
ifstream: Stream class to read from files
fstream: Stream class to both read and write from/to files.
EXCEPTION HANDLING
• One of the advantages of C++ over C is Exception Handling. Exceptions are runtime anomalies or
abnormal conditions that a program encounters during its execution.
• There are two types of exceptions: a)Synchronous, b)Asynchronous (i.e., exceptions which are beyond
the program’s control, such as disc failure, keyboard interrupts etc.).
• C++ provides the following specialized keywords for this purpose:
try: Represents a block of code that can throw an exception.
catch: Represents a block of code that is executed when a particular exception is thrown.
throw: Used to throw an exception. Also used to list the exceptions that a function throws but doesn’t
handle itself.
C++ EXCEPTIONS:
• When executing C++ code, different errors can occur: coding errors made by the programmer, errors
due to wrong input, or other unforeseeable things.
• When an error occurs, C++ will normally stop and generate an error message. The technical term for
this is: C++ will throw an exception (error).
C++ TRY AND CATCH:
• Exception handling in C++ consists of three keywords: try, throw and catch:
• The try statement allows you to define a block of code to be tested for errors while it is being executed.
• The throw keyword throws an exception when a problem is detected, which lets us create a custom
error.
• The catch statement allows you to define a block of code to be executed if an error occurs in the try
block.
EXAMPLE
• #include <iostream>
• using namespace std;
•
• int main()
• {
• int x = -1;
•
• // Some code
• cout << "Before try n";
• try {
CONT…..
• cout << "Inside try n";
• if (x < 0)
• {
• throw x;
• cout << "After throw (Never executed) n";
• }
• }
CONT….
• catch (int x ) {
• cout << "Exception Caught n";
• }
•
• cout << "After catch (Will be executed) n";
• return 0;
• }
FILES I/O
• C++ provides the following classes to perform output and input of characters to/from files:
• ofstream: Stream class to write on files
• ifstream: Stream class to read from files
• fstream: Stream class to both read and write from/to files.
• These classes are derived directly or indirectly from the classes istream and ostream.
• We have already used objects whose types were these classes: cin is an object of class istream and cout is an object of class
ostream.
• Therefore, we have already been using classes that are related to our file streams.
• And in fact, we can use our file streams the same way we are already used to use cin and cout, with the only difference that
we have to associate these streams with physical files.
// BASIC FILE OPERATIONS
• #include <iostream>
• #include <fstream>
• using namespace std;
• int main () {
• ofstream myfile;
• myfile.open ("example.txt");
• myfile << "Writing this to a file.n";
• myfile.close();
• return 0;
• }
OPEN A FILE
• The first operation generally performed on an object of one of these classes is to associate it to a real
file. This procedure is known as to open a file. An open file is represented within a program by a stream
(i.e., an object of one of these classes; in the previous example, this was myfile) and any input or output
operation performed on this stream object will be applied to the physical file associated to it.
• In order to open a file with a stream object we use its member function open:
• open (filename, mode);
CONT…..
• ios::in Open for input operations.
• ios::out Open for output operations.
• ios::binary Open in binary mode.
• ios::ate Set the initial position at the end of the file.
• If this flag is not set, the initial position is the beginning of the file.
• ios::app All output operations are performed at the end of the file, appending the content to the
current content of the file.
• ios::trunc If the file is opened for output operations and it already existed, its previous content is
deleted and replaced by the new one.
EXCEPTION HANDLING(EXCEPTION HANDLING MECHANISM,
THROWING MECHANISM, CATCHING MECHANISM, RE-
THROWING AN EXCEPTION)
• An exception is a problem that arises during the execution of a program. A C++ exception is a response
to an exceptional circumstance that arises while a program is running, such as an attempt to divide by
zero.
• Exceptions provide a way to transfer control from one part of a program to another. C++ exception
handling is built upon three keywords: try, catch, and throw.
CONT…..
• throw − A program throws an exception when a problem shows up. This is done using a throw keyword.
• catch − A program catches an exception with an exception handler at the place in a program where you
want to handle the problem. The catch keyword indicates the catching of an exception.
• try − A try block identifies a block of code for which particular exceptions will be activated. It's followed
by one or more catch blocks.
THROWING EXCEPTIONS
• Exceptions can be thrown anywhere within a code block using throw statement. The operand of the
throw statement determines a type for the exception and can be any expression and the type of the
result of the expression determines the type of exception thrown.
• Following is an example of throwing an exception when dividing by zero condition occurs −
• double division(int a, int b) {
• if( b == 0 ) {
• throw "Division by zero condition!";
• }
• return (a/b);
• }
EXAMPLE
• #include <iostream>
• using namespace std;
• double division(int a, int b) {
• if( b == 0 ) {
• throw "Division by zero condition!";
• }
• return (a/b);
• }
CONT…..
• int main () {
• int x = 50;
• int y = 0;
• double z = 0;
• try {
• z = division(x, y);
CONT……
• cout << z << endl;
• } catch (const char* msg) {
• cerr << msg << endl;
• }
• return 0;
• }
THANK YOU

More Related Content

Similar to OOPS & C++ File Handling, Exception Handling, and Streams

06 file processing
06 file processing06 file processing
06 file processingIssay Meii
 
C++ - UNIT_-_V.pptx which contains details about File Concepts
C++  - UNIT_-_V.pptx which contains details about File ConceptsC++  - UNIT_-_V.pptx which contains details about File Concepts
C++ - UNIT_-_V.pptx which contains details about File ConceptsANUSUYA S
 
Linux System Programming - File I/O
Linux System Programming - File I/O Linux System Programming - File I/O
Linux System Programming - File I/O YourHelper1
 
Exception Handling ,templates in C++
Exception Handling ,templates in C++Exception Handling ,templates in C++
Exception Handling ,templates in C++jamilmalik19
 
Tutorial on-python-programming
Tutorial on-python-programmingTutorial on-python-programming
Tutorial on-python-programmingChetan Giridhar
 
Chapter 13_m5JAVANOTESAPPLETS,INPUT.pptx
Chapter 13_m5JAVANOTESAPPLETS,INPUT.pptxChapter 13_m5JAVANOTESAPPLETS,INPUT.pptx
Chapter 13_m5JAVANOTESAPPLETS,INPUT.pptxnoonoboom
 
Linux System Programming - Advanced File I/O
Linux System Programming - Advanced File I/OLinux System Programming - Advanced File I/O
Linux System Programming - Advanced File I/OYourHelper1
 
Stream classes in C++
Stream classes in C++Stream classes in C++
Stream classes in C++Shyam Gupta
 
FILE OPERATIONS.pptx
FILE OPERATIONS.pptxFILE OPERATIONS.pptx
FILE OPERATIONS.pptxDeepasCSE
 
File handling in_c
File handling in_cFile handling in_c
File handling in_csanya6900
 

Similar to OOPS & C++ File Handling, Exception Handling, and Streams (20)

Files and streams
Files and streamsFiles and streams
Files and streams
 
41c
41c41c
41c
 
Os lectures
Os lecturesOs lectures
Os lectures
 
06 file processing
06 file processing06 file processing
06 file processing
 
C++ - UNIT_-_V.pptx which contains details about File Concepts
C++  - UNIT_-_V.pptx which contains details about File ConceptsC++  - UNIT_-_V.pptx which contains details about File Concepts
C++ - UNIT_-_V.pptx which contains details about File Concepts
 
Filehadnling
FilehadnlingFilehadnling
Filehadnling
 
JAVA (UNIT 3)
JAVA (UNIT 3)JAVA (UNIT 3)
JAVA (UNIT 3)
 
Linux System Programming - File I/O
Linux System Programming - File I/O Linux System Programming - File I/O
Linux System Programming - File I/O
 
Java I/O
Java I/OJava I/O
Java I/O
 
Exception Handling ,templates in C++
Exception Handling ,templates in C++Exception Handling ,templates in C++
Exception Handling ,templates in C++
 
Systemcall1
Systemcall1Systemcall1
Systemcall1
 
PROGRAMMING IN JAVA
PROGRAMMING IN JAVAPROGRAMMING IN JAVA
PROGRAMMING IN JAVA
 
Tutorial on-python-programming
Tutorial on-python-programmingTutorial on-python-programming
Tutorial on-python-programming
 
Exception
ExceptionException
Exception
 
Chapter 13_m5JAVANOTESAPPLETS,INPUT.pptx
Chapter 13_m5JAVANOTESAPPLETS,INPUT.pptxChapter 13_m5JAVANOTESAPPLETS,INPUT.pptx
Chapter 13_m5JAVANOTESAPPLETS,INPUT.pptx
 
Linux System Programming - Advanced File I/O
Linux System Programming - Advanced File I/OLinux System Programming - Advanced File I/O
Linux System Programming - Advanced File I/O
 
Stream classes in C++
Stream classes in C++Stream classes in C++
Stream classes in C++
 
FILE OPERATIONS.pptx
FILE OPERATIONS.pptxFILE OPERATIONS.pptx
FILE OPERATIONS.pptx
 
File handling in_c
File handling in_cFile handling in_c
File handling in_c
 
ch09.ppt
ch09.pptch09.ppt
ch09.ppt
 

More from SURBHI SAROHA

Cloud Computing (Infrastructure as a Service)UNIT 2
Cloud Computing (Infrastructure as a Service)UNIT 2Cloud Computing (Infrastructure as a Service)UNIT 2
Cloud Computing (Infrastructure as a Service)UNIT 2SURBHI SAROHA
 
Management Information System(Unit 2).pptx
Management Information System(Unit 2).pptxManagement Information System(Unit 2).pptx
Management Information System(Unit 2).pptxSURBHI SAROHA
 
Searching in Data Structure(Linear search and Binary search)
Searching in Data Structure(Linear search and Binary search)Searching in Data Structure(Linear search and Binary search)
Searching in Data Structure(Linear search and Binary search)SURBHI SAROHA
 
Management Information System(UNIT 1).pptx
Management Information System(UNIT 1).pptxManagement Information System(UNIT 1).pptx
Management Information System(UNIT 1).pptxSURBHI SAROHA
 
Introduction to Cloud Computing(UNIT 1).pptx
Introduction to Cloud Computing(UNIT 1).pptxIntroduction to Cloud Computing(UNIT 1).pptx
Introduction to Cloud Computing(UNIT 1).pptxSURBHI SAROHA
 
Keys in dbms(UNIT 2)
Keys in dbms(UNIT 2)Keys in dbms(UNIT 2)
Keys in dbms(UNIT 2)SURBHI SAROHA
 
Database Management System(UNIT 1)
Database Management System(UNIT 1)Database Management System(UNIT 1)
Database Management System(UNIT 1)SURBHI SAROHA
 
Object-Oriented Programming with Java UNIT 1
Object-Oriented Programming with Java UNIT 1Object-Oriented Programming with Java UNIT 1
Object-Oriented Programming with Java UNIT 1SURBHI SAROHA
 
Database Management System(UNIT 1)
Database Management System(UNIT 1)Database Management System(UNIT 1)
Database Management System(UNIT 1)SURBHI SAROHA
 
OOPS USING C++(UNIT 2)
OOPS USING C++(UNIT 2)OOPS USING C++(UNIT 2)
OOPS USING C++(UNIT 2)SURBHI SAROHA
 
Object Oriented Programming using C++(UNIT 1)
Object Oriented Programming using C++(UNIT 1)Object Oriented Programming using C++(UNIT 1)
Object Oriented Programming using C++(UNIT 1)SURBHI SAROHA
 

More from SURBHI SAROHA (20)

Cloud Computing (Infrastructure as a Service)UNIT 2
Cloud Computing (Infrastructure as a Service)UNIT 2Cloud Computing (Infrastructure as a Service)UNIT 2
Cloud Computing (Infrastructure as a Service)UNIT 2
 
Management Information System(Unit 2).pptx
Management Information System(Unit 2).pptxManagement Information System(Unit 2).pptx
Management Information System(Unit 2).pptx
 
Searching in Data Structure(Linear search and Binary search)
Searching in Data Structure(Linear search and Binary search)Searching in Data Structure(Linear search and Binary search)
Searching in Data Structure(Linear search and Binary search)
 
Management Information System(UNIT 1).pptx
Management Information System(UNIT 1).pptxManagement Information System(UNIT 1).pptx
Management Information System(UNIT 1).pptx
 
Introduction to Cloud Computing(UNIT 1).pptx
Introduction to Cloud Computing(UNIT 1).pptxIntroduction to Cloud Computing(UNIT 1).pptx
Introduction to Cloud Computing(UNIT 1).pptx
 
JAVA (UNIT 5)
JAVA (UNIT 5)JAVA (UNIT 5)
JAVA (UNIT 5)
 
DBMS (UNIT 5)
DBMS (UNIT 5)DBMS (UNIT 5)
DBMS (UNIT 5)
 
DBMS UNIT 4
DBMS UNIT 4DBMS UNIT 4
DBMS UNIT 4
 
JAVA(UNIT 4)
JAVA(UNIT 4)JAVA(UNIT 4)
JAVA(UNIT 4)
 
OOPS & C++(UNIT 4)
OOPS & C++(UNIT 4)OOPS & C++(UNIT 4)
OOPS & C++(UNIT 4)
 
DBMS UNIT 3
DBMS UNIT 3DBMS UNIT 3
DBMS UNIT 3
 
Keys in dbms(UNIT 2)
Keys in dbms(UNIT 2)Keys in dbms(UNIT 2)
Keys in dbms(UNIT 2)
 
DBMS (UNIT 2)
DBMS (UNIT 2)DBMS (UNIT 2)
DBMS (UNIT 2)
 
JAVA UNIT 2
JAVA UNIT 2JAVA UNIT 2
JAVA UNIT 2
 
Database Management System(UNIT 1)
Database Management System(UNIT 1)Database Management System(UNIT 1)
Database Management System(UNIT 1)
 
Object-Oriented Programming with Java UNIT 1
Object-Oriented Programming with Java UNIT 1Object-Oriented Programming with Java UNIT 1
Object-Oriented Programming with Java UNIT 1
 
Database Management System(UNIT 1)
Database Management System(UNIT 1)Database Management System(UNIT 1)
Database Management System(UNIT 1)
 
OOPs & C++ UNIT 3
OOPs & C++ UNIT 3OOPs & C++ UNIT 3
OOPs & C++ UNIT 3
 
OOPS USING C++(UNIT 2)
OOPS USING C++(UNIT 2)OOPS USING C++(UNIT 2)
OOPS USING C++(UNIT 2)
 
Object Oriented Programming using C++(UNIT 1)
Object Oriented Programming using C++(UNIT 1)Object Oriented Programming using C++(UNIT 1)
Object Oriented Programming using C++(UNIT 1)
 

Recently uploaded

Final demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxFinal demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxAvyJaneVismanos
 
Biting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfBiting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfadityarao40181
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsanshu789521
 
ENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptx
ENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptxENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptx
ENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptxAnaBeatriceAblay2
 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Celine George
 
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxRaymartEstabillo3
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application ) Sakshi Ghasle
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
Painted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaPainted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaVirag Sontakke
 
Pharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfPharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfMahmoud M. Sallam
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformChameera Dedduwage
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionSafetyChain Software
 
Science 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsScience 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsKarinaGenton
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdfssuser54595a
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon AUnboundStockton
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13Steve Thomason
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfSumit Tiwari
 

Recently uploaded (20)

Final demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxFinal demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptx
 
Biting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfBiting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdf
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha elections
 
ENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptx
ENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptxENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptx
ENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptx
 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17
 
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application )
 
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
Painted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaPainted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of India
 
Pharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfPharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdf
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory Inspection
 
Science 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsScience 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its Characteristics
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon A
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
 

OOPS & C++ File Handling, Exception Handling, and Streams

  • 1. OOPS & C++(UNIT 5) BY:SURBHI SAROHA
  • 2. SYLLABUS • File Handling • Exception Handling • Files I/O • Exception Handling(Exception Handling Mechanism, Throwing Mechanism, Catching Mechanism, Re- throwing an Exception)
  • 3. FILE HANDLING • 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.
  • 4. CONT….. • 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”.
  • 5. 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. These classes, designed to manage the disk files, are declared in fstream and therefore we must include this file in any program that uses files. 1. ios:- • ios stands for input output stream. • This class is the base class for other classes in this class hierarchy. • This class contains the necessary facilities that are used by all the other derived classes for input and output operations.
  • 6. CONT….. • 2. istream:- • istream stands for input stream. • This class is derived from the class ‘ios’. • This class handle input stream. • The extraction operator(>>) is overloaded in this class to handle input streams from files to the program execution. • This class declares input functions such as get(), getline() and read().
  • 7. CONT….. • 3. ostream:- • ostream stands for output stream. • This class is derived from the class ‘ios’. • This class handle output stream. • The insertion operator(<<) is overloaded in this class to handle output streams to files from the program execution. • This class declares output functions such as put() and write(). • 4. streambuf:- • This class contains a pointer which points to the buffer which is used to manage the input and output streams.
  • 8. CONT….. • 5. fstreambase:- • This class provides operations common to the file streams. Serves as a base for fstream, ifstream and ofstream class. • This class contains open() and close() function. • 6. ifstream:- • This class provides input operations. • It contains open() function with default input mode. • Inherits the functions get(), getline(), read(), seekg() and tellg() functions from the istream. • 7. ofstream:- • This class provides output operations. • It contains open() function with default output mode.
  • 9. CONT…… • Inherits the functions put(), write(), seekp() and tellp() functions from the ostream. • 8. fstream:- • This class provides support for simultaneous input and output operations. • Inherits all the functions from istream and ostream classes through iostream. • 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.
  • 10. CONT….. • In C++, files are mainly dealt by using three classes fstream, ifstream, ofstream available in fstream headerfile. ofstream: Stream class to write on files ifstream: Stream class to read from files fstream: Stream class to both read and write from/to files.
  • 11. EXCEPTION HANDLING • One of the advantages of C++ over C is Exception Handling. Exceptions are runtime anomalies or abnormal conditions that a program encounters during its execution. • There are two types of exceptions: a)Synchronous, b)Asynchronous (i.e., exceptions which are beyond the program’s control, such as disc failure, keyboard interrupts etc.). • C++ provides the following specialized keywords for this purpose: try: Represents a block of code that can throw an exception. catch: Represents a block of code that is executed when a particular exception is thrown. throw: Used to throw an exception. Also used to list the exceptions that a function throws but doesn’t handle itself.
  • 12. C++ EXCEPTIONS: • When executing C++ code, different errors can occur: coding errors made by the programmer, errors due to wrong input, or other unforeseeable things. • When an error occurs, C++ will normally stop and generate an error message. The technical term for this is: C++ will throw an exception (error).
  • 13. C++ TRY AND CATCH: • Exception handling in C++ consists of three keywords: try, throw and catch: • The try statement allows you to define a block of code to be tested for errors while it is being executed. • The throw keyword throws an exception when a problem is detected, which lets us create a custom error. • The catch statement allows you to define a block of code to be executed if an error occurs in the try block.
  • 14. EXAMPLE • #include <iostream> • using namespace std; • • int main() • { • int x = -1; • • // Some code • cout << "Before try n"; • try {
  • 15. CONT….. • cout << "Inside try n"; • if (x < 0) • { • throw x; • cout << "After throw (Never executed) n"; • } • }
  • 16. CONT…. • catch (int x ) { • cout << "Exception Caught n"; • } • • cout << "After catch (Will be executed) n"; • return 0; • }
  • 17. FILES I/O • C++ provides the following classes to perform output and input of characters to/from files: • ofstream: Stream class to write on files • ifstream: Stream class to read from files • fstream: Stream class to both read and write from/to files. • These classes are derived directly or indirectly from the classes istream and ostream. • We have already used objects whose types were these classes: cin is an object of class istream and cout is an object of class ostream. • Therefore, we have already been using classes that are related to our file streams. • And in fact, we can use our file streams the same way we are already used to use cin and cout, with the only difference that we have to associate these streams with physical files.
  • 18. // BASIC FILE OPERATIONS • #include <iostream> • #include <fstream> • using namespace std; • int main () { • ofstream myfile; • myfile.open ("example.txt"); • myfile << "Writing this to a file.n"; • myfile.close(); • return 0; • }
  • 19. OPEN A FILE • The first operation generally performed on an object of one of these classes is to associate it to a real file. This procedure is known as to open a file. An open file is represented within a program by a stream (i.e., an object of one of these classes; in the previous example, this was myfile) and any input or output operation performed on this stream object will be applied to the physical file associated to it. • In order to open a file with a stream object we use its member function open: • open (filename, mode);
  • 20. CONT….. • ios::in Open for input operations. • ios::out Open for output operations. • ios::binary Open in binary mode. • ios::ate Set the initial position at the end of the file. • If this flag is not set, the initial position is the beginning of the file. • ios::app All output operations are performed at the end of the file, appending the content to the current content of the file. • ios::trunc If the file is opened for output operations and it already existed, its previous content is deleted and replaced by the new one.
  • 21. EXCEPTION HANDLING(EXCEPTION HANDLING MECHANISM, THROWING MECHANISM, CATCHING MECHANISM, RE- THROWING AN EXCEPTION) • An exception is a problem that arises during the execution of a program. A C++ exception is a response to an exceptional circumstance that arises while a program is running, such as an attempt to divide by zero. • Exceptions provide a way to transfer control from one part of a program to another. C++ exception handling is built upon three keywords: try, catch, and throw.
  • 22. CONT….. • throw − A program throws an exception when a problem shows up. This is done using a throw keyword. • catch − A program catches an exception with an exception handler at the place in a program where you want to handle the problem. The catch keyword indicates the catching of an exception. • try − A try block identifies a block of code for which particular exceptions will be activated. It's followed by one or more catch blocks.
  • 23. THROWING EXCEPTIONS • Exceptions can be thrown anywhere within a code block using throw statement. The operand of the throw statement determines a type for the exception and can be any expression and the type of the result of the expression determines the type of exception thrown. • Following is an example of throwing an exception when dividing by zero condition occurs − • double division(int a, int b) { • if( b == 0 ) { • throw "Division by zero condition!"; • } • return (a/b); • }
  • 24. EXAMPLE • #include <iostream> • using namespace std; • double division(int a, int b) { • if( b == 0 ) { • throw "Division by zero condition!"; • } • return (a/b); • }
  • 25. CONT….. • int main () { • int x = 50; • int y = 0; • double z = 0; • try { • z = division(x, y);
  • 26. CONT…… • cout << z << endl; • } catch (const char* msg) { • cerr << msg << endl; • } • return 0; • }