PF-CE Lab09 File Handeling - 2
PF-CE Lab09 File Handeling - 2
Fundamentals
(CL1002)
LABORATORY MANUAL
BS-CE, Spring 2025
LAB 09
File Handling - 2
____________Manahil
shehzad_________________________ ___6563_______
__CE -B__
STUDENT NAME ROLL NO SEC
LAB ENGINEER'S SIGNATURE & DATE
Maryam Zafar
______________________________________
_____________________________________________________________________
NATIONAL UNIVERSITY OF COMPUTER AND EMERGING SCIENCES (NUCES), ISLAMABAD
Prepared by: Engr. Aamer Munir, Muhammad Hammad Version: 1.1 Last edited by: Engr. Azhar Rauf,
Lab Objectives:
1. To learn how to write to and read from in sequential as well as random access
Software Required:
● Code: Dev C++
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 (Insertion) << and (Extraction) >> operators exactly like
cout/cin.
• 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.
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 modes is append as explained below:
Mode Description
ios::app All output operations are performed at the end of the file, appending
the content 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;
}
char array[1000];
ifstream read("filename.txt");
while(!read.eof())
{
read>> array[i];
i++;
} //get only formatted input and skips whitespace characters
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();
Example 7:
#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;
}
PF LAB 09 NUCES, ISLAMABAD Page 4 of 10
Tasks
Task01: Assume the file input.txt contains the following characters:
Describe the difference between reading a file with the >> operator and the
getline function.
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
fstream inFile("input.txt", ios::in);
string item;
inFile >> item;
while (inFile)
{
cout << item << endl;
inFile >> item;
}
inFile.close();
return 0;
}
PF LAB 09 NUCES, ISLAMABAD Page 5 of 10
Task02: What will be stored in the file out.txt after the following program runs?
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
int main()
{
const int SIZE = 5;
ofstream outFile("out.txt");
double nums[SIZE] = {100.279, 1.719, 8.602, 7.777, 5.099};
outFile << fixed << setprecision(2);
for (int count = 0; count < 5; count++)
{
outFile << setw(8) << nums[count];
}
outFile.close();
return 0;
}
Task03:
I am student of Computer Engineering at Fast University. My name is xyz. We are studying File
handling in C++. This is my first file created by C++ program. The Purpose file is to store the
data and then to provide it to C++ program for reading purpose. Objective is that C++ Program
should read the complete sentences.
Read in the complete sentences from the file.txt and display it on console (screen). Write code that
determines the number of bytes contained in the file associated with
the file stream object dataFile.
PF LAB 09 NUCES, ISLAMABAD Page 7 of 10
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main() {
ofstream outFile("file.txt");
outFile.close();
return 0;
}
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
ifstream inFile("file.txt");
string line;
cout << "Content of the file.txt:" << endl;
inFile.close();
inFile.open("file.txt", ios::in);
inFile.seekg(0, ios::end);
int fileSize = inFile.tellg();
cout << "The file contains " << fileSize << " bytes." << endl;
inFile.close();
return 0;
return 0;
}
PF LAB 09 NUCES, ISLAMABAD Page 8 of 10
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main(){
return 0;
}
Task05: Using the file created in Task 03, display the odd characters of every word (if word length
>2) . Store the result in a new file called oddfile.txt
#include <iostream>
#include <fstream>
#include <string>
int main() {
ifstream inFile("file.txt");
ofstream outFile ("Oddwords.txt");
string Fast;
while(inFile>>Fast)
{
if(Fast.length()>2)
{
for(int i=0;i < Fast.length();i+=2)
{
outFile << Fast[i];
}
outFile<<" ";
}
}
inFile.close();
outFile.close();
return 0;
}