0% found this document useful (0 votes)
41 views5 pages

OOP Lab 903 File Handling

This document contains a laboratory manual for a file handling lab. The objectives are to learn how to write to and read from text files in C++. It introduces file handling using fstream, text and binary file formats, file modes like append, and examples of writing to and reading from a text file. It also covers closing files and provides tasks to read and manipulate a text file, create a student information system that writes to a file, and displays the file data.

Uploaded by

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

OOP Lab 903 File Handling

This document contains a laboratory manual for a file handling lab. The objectives are to learn how to write to and read from text files in C++. It introduces file handling using fstream, text and binary file formats, file modes like append, and examples of writing to and reading from a text file. It also covers closing files and provides tasks to read and manipulate a text file, create a student information system that writes to a file, and displays the file data.

Uploaded by

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

Object Oriented Programming

(CL1004)
LABORATORY MANUAL
Fall 2023

LAB 03
File Handling
Engr. Arslan Ahmed

________________________________________ __________ ___


STUDENT NAME ROLL NO SEC

______________________________________
LAB ENGINEER SIGNATURE & DATE

MARKS AWARDED: ___/10

NATIONAL UNIVERSITY OF COMPUTER AND EMERGING SCIENCES (NUCES), ISLAMABAD


File Handling LAB 03

LAB 03 File Handling

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.

2. Text and Binary File Formats


• The number -3.7182193 can either be a double or float depending on data type used.
• For example float f = -3.7182193; is float and takes 4 bytes for storage in memory. These
4 bytes contain the binary representation of float.
• If stored as a string, it is 10 characters long including the minus sign and decimal point:
char c[] = "-3.7182193";. Note: NULL is implicit and does not count towards length of
string.
• Take another example of an integer int x = 1; If stored as a string of characters, it will take
only 1 byte. If stored as an integer it will take 4 bytes, regardless of the value.
• In a text file, data is stored as a string of characters. It is a copy of what you see on your
console. As can be expected, a text file is easily readable if opened in a text editor.
• In a binary file, data is stored as it would in a memory. It is a copy of what is in the memory.
A binary file, just like memory, is not so obviously readable in raw form.

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.

4. Example: Writing to Text File

Fall 2023: OOP Lab NUCES, ISLAMABAD Page 2 of 5


File Handling LAB 03

#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;
}

5. Example: Reading from Text File

#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;
}

Fall 2023: OOP Lab NUCES, ISLAMABAD Page 3 of 5


File Handling LAB 03

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.

How to close the 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;
}

Fall 2023: OOP Lab NUCES, ISLAMABAD Page 4 of 5


File Handling LAB 03

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

Fall 2023: OOP Lab NUCES, ISLAMABAD Page 5 of 5

You might also like