0% found this document useful (0 votes)
3 views11 pages

PF-CE Lab09 File Handeling - 2

This laboratory manual for Programming Fundamentals (CL1002) focuses on file handling in C++. It includes objectives, examples of writing and reading text files, and tasks for students to practice file operations using C++. The manual is prepared for the BS-CE program at NUCES, Islamabad, and outlines various coding exercises related to file input/output.

Uploaded by

Mariya Nawaz
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)
3 views11 pages

PF-CE Lab09 File Handeling - 2

This laboratory manual for Programming Fundamentals (CL1002) focuses on file handling in C++. It includes objectives, examples of writing and reading text files, and tasks for students to practice file operations using C++. The manual is prepared for the BS-CE program at NUCES, Islamabad, and outlines various coding exercises related to file input/output.

Uploaded by

Mariya Nawaz
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/ 11

Programming

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

MARKS AWARDED: /10

______________________________________
_____________________________________________________________________
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,

Engr. Aamer MunirDate last

edited:Sep 27, 2018

Verified by: Engr. Azhar RaufDate last edited:Jan 10, 2025

File Handling -2 LAB 09

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.

2. Text 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.

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.

4. Example 1: Writing to Text File


PF LAB 09 NUCES, ISLAMABAD Page 2 of 10

File Handling -2 LAB 09

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

● We can also read data in a char Array:

char array[1000];
ifstream read("filename.txt");

read>> array; //read one word


PF LAB 09 NUCES, ISLAMABAD Page 3 of 10

File Handling -2 LAB 09

read.getline(array,1000); //read one line

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.

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

File Handling -2 LAB 09

Tasks
Task01: Assume the file input.txt contains the following characters:

What will the following program display on the screen?


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

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

File Handling -2 LAB 09


#include<iostream>
#include<fstream>
#include<string>
using namespace std;
int main()
{

fstream inFile("Runn.txt", ios::in);


string item;
getline(inFile,item);
while (inFile)
{
cout << item << endl;
getline(inFile,item);
}
inFile.close();
return 0;
}

PF LAB 09 NUCES, ISLAMABAD Page 6 of 10

File Handling -2 LAB 09

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:

Create the file.txt using C++ Program and store

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

File Handling -2 LAB 09

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main() {
ofstream outFile("file.txt");

outFile << "I am a 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 a C++ program. "
<< "The purpose of the file is to store data and then provide it "
<< "to the C++ program for reading purposes. The objective is that the C++ program "
<< "should read the complete sentences." << endl;

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;

while (getline(inFile, line)) {


cout << line << 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

File Handling -2 LAB 09


Task04: Using the file created in Task 03, display the file content in reverse order using Seek and Tell
functions only.

#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main(){

ifstream inFile ("file.txt");


inFile.seekg(0, ios::end);

long fileSize = inFile.tellg();

cout << "Content of the file in reverse order:" << endl;

for (long i = fileSize - 1; i >= 0; --i) {


inFile.seekg(i);
char ch;
inFile.get(ch);
cout << ch;
}

cout << endl;


inFile.close();

return 0;
}

PF LAB 09 NUCES, ISLAMABAD Page 9 of 10


File Handling -2 LAB 09

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>

using namespace std;

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

PF LAB 09 NUCES, ISLAMABAD Page 10 of 10

You might also like