You are on page 1of 11

Programming Fundamentals

Lab-13

For Students:

Roll No

Name

Class

Instructor

_________________________
Mr. Irfan Ullah

_____________________________________________________________________________1
39
Programming Fundamentals - Lab [COSC-1201]
13.1. OBJECTIVES

1. File Handling
a. Input / Output with Files

13.2. File Handling

Programs in C++ store data in variables. The data stored is temporary.


When a program is terminated, the entire data is lost. Storing in a file
will preserve your data even if the program terminates.

With file handling we can store large amount of data permanently.


There are two types of files we can work with in c++:

i. Text Files
Text files are the normal .txt files that you can easily create using Notepad or any simple text
editors. They take minimum effort to maintain, are easily readable, and provide least security
and takes bigger storage space.

ii. Binary Files


Binary files are mostly the .bin files in your computer. Instead of storing data in plain text,
they store it in the binary form (0's and 1's). They can hold higher amount of data, are not
readable easily and provides a better security than text files.

13.2.1. File Operations

We can perform four major operations on the file, either text or binary:

1. Creating a new file


2. Opening an existing file
3. Closing a file
4. Reading from and writing information to a file

13.2.2. Input / Output with Files

C++ provides the following classes to perform output and input of characters to/from files:

ofstream: Stream class to write on files


ifstream: Stream class to read from files
fstream: Stream class to both read and write from/to files.

_____________________________________________________________________________1
40
Programming Fundamentals - Lab [COSC-1201]
To work with files, we will always include fstream header file in our
programs. The following program describes the basic functionality to
create a file, write into file, and read from a file.

13.2.3. Opening a File:

A file should be opened before it can be processed. A file can be


opened by first creating a stream object in the program. Any input /
output operation performed on this stream object is applied to the
physical file associated with it.

In order to open a file with a stream object we use its member function
open:

Open(filename, mode);

Where file name is the name of the file, and mode is an optional
parameter.

If we want to open a file i.e., afile.txt, we can use the built in function
open() of the stream object. The syntax is:
1. ofstream outfile;
2. outfile.open("afile.txt");

Each of the open member functions of classes ofstream, ifstream and


fstream has a default mode that is used if the file is opened without a
second argument:
Class Default opening mode
Ofstream Ios::out
Ifstream Ios::in
Fstream Ios::in | ios::out

For ifstream and ofstream classes, ios::in and ios::out are


automatically and respectively assumed, even if a mode that does not
include them is passed as a second argument.

For fstream, the default value is only applied if the function is called
without specifying any value for the mode parameter. If the function is
_____________________________________________________________________________1
41
Programming Fundamentals - Lab [COSC-1201]
called with any value in that parameter the default mode is overridden,
not combined.

We can also open a file with these optional modes:


Opening Working
mode
ios::app All output operations are performed at the end of file,
appending the content to the current content of file.
ios::trunc If the file is opened for output operations and it already
existed, its previous content is deleted and replaced by
the new one.

Example # 1: A simple program to input text in to a file, and output


text from a file.
#include<iostream>
#include<fstream>
using namespace std;

int main()
{ char array[50];
fstream object;
object.open("newfile.txt",ios::in|ios::out|ios::app);

if(object.is_open())
{ object<<"I am new to learning file handling \n";
object.close();
cout<<"Success \n";
}
else
cout<<"File opening error";

object.open("newfile.txt");

cout<<"Now reading from file \n";


while(!object.eof())
{ object.getline(array,50);
_____________________________________________________________________________1
42
Programming Fundamentals - Lab [COSC-1201]
cout<<array <<"\n";
}
object.close();
}

Example # 2: A simple program to input text in to a Binary file, and


output text from a Binary file.
#include<iostream>
#include<fstream>
using namespace std;
int main()
{
char array[50];
cout<<"name";
cin.getline(array,50);

ofstream file;
file.open("data.txt",ios::binary|ios::app);
if(!file){
cout<<"Error opeing file";}
else{
file.write((char*)&array, sizeof(array));
file.close();
}

ifstream file1;
file1.open("data.txt");
if(!file1)
{cout<<"File opening error";
file1.close();
}
while(!file1.eof())
{
file1.read((char*)&array, sizeof(array));
cout<<"File says"<<array<<endl;
}

_____________________________________________________________________________1
43
Programming Fundamentals - Lab [COSC-1201]
file1.close();
}

Example # 3: A simple program to input text in to a Binary file, and


output text from a Binary file by searching.
#include<iostream>
#include<fstream>
#include<string.h>
using namespace std;
struct person{ char name[50];
int age;
int salary;
}p;

void addrecord()
{ cout<<"Enter name";
cin.getline(p.name,50);

cout<<"Enter Age";
cin>>p.age;

cout<<"Enter Salary";
cin>>p.salary;
}

void showperson(char A[])


{
ifstream rfile("person.txt");
if(!rfile.is_open())
{cout<<"File not open";
rfile.close();
}

while(rfile.read((char *) &p, sizeof(p)))


{

_____________________________________________________________________________1
44
Programming Fundamentals - Lab [COSC-1201]
if(strcmp(A,p.name)==0)
{
cout<<" name is : "<<p.name<<endl;
cout<<"Age is: "<<p.age<<endl;
cout<<"Salary is: "<<p.salary<<endl;
}
}

}
int main()
{
ofstream wfile("person.txt", ios::binary|ios::app);
if(!wfile.is_open())
{cout<<"File not open";
wfile.close();
}
addrecord();

wfile.write((char *)&p,sizeof(p));
cout<<"Person added";
wfile.close();

char Arr[30];
cout<<"Enter person name you want to search";
cin.ignore();
cin.getline(Arr, 30);

showperson(Arr);
}

13.3. LAB ACTIVITIES


13.3.1. Program # 1

_____________________________________________________________________________1
45
Programming Fundamentals - Lab [COSC-1201]
Write a program that inputs upto 10 integer values in to a file, and then
displays them on screen.

_____________________________________________________________________________1
46
Programming Fundamentals - Lab [COSC-1201]
13.3.2. Program # 2

Write a program that inputs character from the user and appends it in
existing file. The input ends if the user enters a full stop.

13.3.3. Program # 3

Write a program that copies the contents of one file to another file.

_____________________________________________________________________________1
47
Programming Fundamentals - Lab [COSC-1201]
13.3.4. Program # 4

Implement and learn Example no 3 given in the same manual.

_____________________________________________________________________________1
48
Programming Fundamentals - Lab [COSC-1201]
_____________________________________________________________________________1
49
Programming Fundamentals - Lab [COSC-1201]

You might also like