You are on page 1of 4

LAB REPORT

Name:
Roll No:
Class:
Lab name:Programming Fundamental
Experiment No.4
File Handling
Introduction:
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.

The term “Data” is commonly referred to as known facts or information. In the present era, data
plays a vital role. It helps to describe, diagnose, predict or prescribe. But to achieve all this, we
need to store it somewhere. You all would argue that there are so many text editors like
‘Notepad’ and ‘MS Office’, which help us store data in the form of text. You are right! But here
we are discussing at a level of programming. In contrast, text editors like ‘Notepad’ and ‘MS
Office’ are pre-built and cannot be accessed at the programming level to store data. File
Handling is a hot topic when it comes to storing such programming data.

Almost every programming language has a ‘File Handling’ method to deal with the storage of
data. In this article, we will learn about file handling in C++

Objectives:
(1) To be able to understand the working of files and functions.
Procedure:
In C++, files are mainly dealt by using three classes fstream, ifstream, ofstream available in
fstream headerfile.

(1)ofstream: Stream class to write on files

(2)ifstream: Stream class to read from files

(3)fstream: Stream class to both read and write from/to files.

Now the first step to open the particular file for read or write operation. We can open file by

1. passing file name in constructor at the time of object creation

2. using the open method

Here we have an iostream library, which is responsible for input/output stream.We also have a
fstream library, which is responsible for handling files.Creating an object of the fstream class
and named it as ‘FileName’.On the above-created object, we have to apply the open() function
to create a new file, and the mode is set to ‘out’ which will allow us to write into the file.We use
the ‘if’ statement to check for the file creation.Prints the message to console if the file doesn’t
exist.Prints the message to console if the file exists/created.We use the close() function on the
object to close the file.

Exercises attepmted in lab

Exercise-1
Coding of Excercise-1
#include<iostream>

#include<fstream>

using namespace std;

void main(){

ifstream in;

in.open("saldina.txt");

ofstream out;

out.open("output_file.txt");
int x;

while (!in.eof())

in>>x;

cout<<x<<"\ \ "<<x*x<<"\ \ "<<x*x*x<<"\ \ "<<endl;

out<<x<<"\ \ "<<x*x<<"\ \ "<<x*x*x<<"\ \ "<<endl;

in.close();

out.close();

Output of exercise-1

Exercise-2
Code of exercise-2

Issues:
I was assigning the wrong file sothe console wasnot showing the correct result.I rechecked my
program and exclude my mistake.

Conclusion:
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. The term
“Data” is commonly referred to as known facts or information. In the present era, data plays a
vital role.

Application:
When a program is terminated, the entire data is lost. Storing in a file will preserve your data
even if the program terminates. If you have to enter a large number of data, it will take a lot of
time to enter them all.

Post lab:
Coding:

You might also like