OOP Lab 903 File Handling
OOP Lab 903 File Handling
(CL1004)
LABORATORY MANUAL
Fall 2023
LAB 03
File Handling
Engr. Arslan Ahmed
______________________________________
LAB ENGINEER SIGNATURE & DATE
Lab Objectives:
• To learn how to write and read from text files.
Software Required:
• Dev C++ / Visual Studio Code
Introduction:
1. File Handling
In C++ file handling library is <fstream>. You create `objects' of type (or class) fstream
and use them for reading to and writing from files.
Text files can be manipulated using << and >> operators exactly like cout/cin.
3. File Modes
Modes are options that can be specified when opening a file. Options are specified as the
second argument of open() function. One of the mode is append as explained below:
Mode Description
All output operations are performed at the end of the file, appending the content
ios::app
to the current content of the file.
#include<iostream>
#include<fstream> //for file I/O
#include<string>
using namespace std;
int main()
{
int x=8;
double y=3.456;
char ch='v';
string str1="ghty";
string str2="tyui";
ofstream outfile("fdata.txt"); //create ofstream object of any name
outfile<<x //insert write data
<<" "
<<y
<<ch
<<str1
<<' '
<<str2;
cout<<"File written"<<endl;
return 0;
}
#include<iostream>
#include<fstream> //for file I/O
#include<string>
using namespace std;
int main()
{
int x;
double y;
char ch;
string str1;
string str2;
ifstream infile("fdata.txt"); //create ifstream object of any name
infile>>x>>y>>ch>>str1>>str2; //Read data
cout<<x<<" "<<y<<" "<<ch<<" "<<str1<<" "<<str2;
return 0;
}
6. File Closing:
A file is closed automatically, when a program is closed. However, it is necessary to explicitly
close the file, if you want to read the file after writing to it in the same program. Therefore, it is
a good habit to close the file whenever you have written something to a file.
fileObject.close();
#include<iostream>
#include<fstream> //for file I/O
#include<string>
using namespace std;
int main()
{
int x=8;
double y=3.456;
char ch='v';
string str1="ghty";
string str2="tyui";
ofstream outfile("fdata.txt"); //create ofstream object of any name
outfile<<x //insert write data
<<" "
<<y
<<ch
<<str1
<<' '
<<str2;
cout<<"File written"<<endl;
outfile.close();
ifstream infile("fdata.txt"); //create ifstream object of any name
infile>>x>>y>>ch>>str1>>str2; //Read data
cout<<x<<" "<<y<<" "<<ch<<" "<<str1<<" "<<str2;
return 0;
}
Task 01: Read a file suramulk.txt and find out the following information:
• Number of words
• Number of paragraphs
• Number of times a word as entered by the user is present in the text
Task 02:
• Task 2.1: Create a student information system, where you will input and save the following
information in a file. Make sure that you do not overwrite the previous information
Roll_No Name City Phone
• Task 2.2: Make a program, which should read the file created in Task 2.1, and display all
the student information in a professional way.
• Task 2.3: Merge the Task 2.1 and Task 2.2 functionality into one program, and present a
menu to the user 1) enter new student 2) view all students