0% found this document useful (0 votes)
2 views27 pages

File Handling

The document provides an overview of file handling and exception handling in C++ for a course on Object Oriented Programming. It explains the use of the fstream library for file operations such as creating, reading, writing, and closing files, as well as the try, catch, and throw keywords for managing exceptions. Key concepts and operations are outlined, including the importance of closing files and handling runtime anomalies.

Uploaded by

f2024065470
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views27 pages

File Handling

The document provides an overview of file handling and exception handling in C++ for a course on Object Oriented Programming. It explains the use of the fstream library for file operations such as creating, reading, writing, and closing files, as well as the try, catch, and throw keywords for managing exceptions. Key concepts and operations are outlined, including the importance of closing files and handling runtime anomalies.

Uploaded by

f2024065470
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 27

Object Oriented Programming

Instructor: Shinawar Naeem


Department of Software Engineering
Semester: Spring 2025
Course Code: CC-1022
File Handling
What is File Handling in C++?
• File handling in C++ is a mechanism to store the output
of a program in a file and help perform various
operations on it. Files help store these data permanently
on a storage device.
File handling is further divided into sub-topics:
• Create a file
• Open a file
• Read from a file
• Write to a file
• Close a file
fstream library
1. Before diving into each sub-topics, let us first learn
about the header file we will be using to gain access to
the file handling method.
2. In C++, fstream library is used to handle files, and it is
dealt with the help of three classes known as
• Ofstream
• Ifstream
• fstream.
• Ofstream- This class helps create and write the data to the
file obtained from the program’s output. It is also known as
the output stream.
• Ifstream- We use this class to read data from files and also
known as the input stream.
• Fstream- This class is the combination of both ofstream
and ifstream. It provides the capability of creating, writing
and reading a file.
• To access the following classes, you must include the fstream as a
header file like how we declare iostream in the header.
File Operations in C++
• C++ provides us with four different operations for file
handling. They are:
1.open() – This is used to create a file.
2.read() – This is used to read the data from the file.
3.write() – This is used to write new data to file.
4.close() – This is used to close the file.
Opening files in C++
• To read or enter data to a file, we need to open it first. This can
be performed with the help of ‘ifstream’ for reading and ‘fstream’
or ‘ofstream’ for writing or appending to the file. All these three
objects have open() function pre-built in them.
• FileName – It denotes the name of file which has to be
opened.
• Mode – There different mode to open a file and it
explained in this article.
Program for Opening File
Writing to File
• Now, we will learn how to write data to file which we
created before. We will use fstream or ofstream object
to write data into the file and to do so; we will use
stream insertion operator (<<) along with the text
enclosed within the double-quotes.
• With the help of open() function, we will create a new
file named ‘FileName’ and then we will set the mode to
‘ios::out’ as we have to write the data to file.
Reading from file in C++

• Here we will use fstream or ifstream

Content of FileName.txt- Hello World, Thank You


for Visiting UMT.
Output
Hello World, Thank
You for Visiting UMT.
• Infile C++(We can also use inFile to read from the file. Here, inFile >> S
takes in the file stream, which is your file data, and uses a space delimiter
(breaks it up by whitespace) and then puts the contents in the variable S.)
• Example:
• If we had a file that had the data:
• “My Cat is Hungry”
• and we used inFile >> S here, i.e.:
Closing a file in C++
• Closing a file is a good practice, and it is must to close
the file. Whenever the C++ program comes to an end, it
clears the allocated memory, and it closes the file. We
can perform the task with the help of close() function.
Exception Handling in C++
Introduction
• Exception Handling. Exceptions are runtime anomalies
or abnormal conditions that a program encounters
during its execution.
• There are two types of exceptions:
1. Synchronous
2. 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.
Why Exception Handling?
• Separation of Error Handling code from Normal Code
• Functions/Methods can handle only the exceptions they
choose
• Grouping of Error Types
C++ Exceptions
• When executing C++ code, different errors can occur:
coding errors made by the programmer, errors due to
wrong input, or other unexpected 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.
The try and catch keywords
come in pairs:
• We use the try block to test some code: If the value of a
variable “age” is less than 18, we will throw an exception, and
handle it in our catch block.
• In the catch block, we catch the error if it occurs and do
something about it. The catch statement takes a single
parameter. So, if the value of age is 15 and that’s why we are
throwing an exception of type int in the try block (age), we can
pass “int myNum” as the parameter to the catch statement,
where the variable “myNum” is used to output the value of age.
• If no error occurs (e.g. if age is 20 instead of 15, meaning it will
be greater than 18), the catch block is skipped.

You might also like