0% found this document useful (0 votes)
21 views15 pages

File Handling

The document provides an overview of file handling, including definitions of files, their advantages, types (text and binary), and access methods (sequential and random). It explains stream concepts, predefined stream objects in C++, file opening modes, and methods for reading and writing data to files, both in formatted and binary I/O. Additionally, it includes example programs demonstrating these concepts in practice.

Uploaded by

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

File Handling

The document provides an overview of file handling, including definitions of files, their advantages, types (text and binary), and access methods (sequential and random). It explains stream concepts, predefined stream objects in C++, file opening modes, and methods for reading and writing data to files, both in formatted and binary I/O. Additionally, it includes example programs demonstrating these concepts in practice.

Uploaded by

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

File Handling

File: A file is a collection of related records. A record contains data about


an entity. The data is stored in files permanently.

Advantages:
Files can store large amount of data permanently.
Files can be updated easily.
One file can be used by many programs for same input.
Files save time and effort to input data via keyboard.

Types:
1) Text files
2) Binary files
Types of Files
1) Text Files
A type of file that stores data as readable and printable characters is
called text file.
2) Binary Files
A type of file that stores data as non-readable binary code is called
binary file.
File Access Methods
The way in which a file can be accessed is called file access method.
Sequential Access Method
Sequential access method is used to access data in the same sequence in which
it is stored in a file. This method reads and writes data in a sequence.
Random Access Method
Random access method is used to access any data item directly without
accessing the preceding data. This method doesn’t read or write data in
sequence.
Stream
A stream is a series of bytes associated with a file. It consists of data that is
transferred from one location to another.

Types of Stream
There are two types of streams
Standard Input Stream
Standard input stream is used to input data. It establishes a connection
between standard input device and a program. The act of reading data from
an input stream is called extraction. It is performed using extraction operator
>> with cin object. The cin is an object of istream and handles input.
Standard Output Stream
Standard output stream is used to output data. It establishes a connection
between standard output device and a program. The act of writing data to an
output stream is called insertion. It is performed using insertion operator <<
with cout object. The cout is an object of ostream and handles output.
Predefined Stream Objects
C++ provides four pre-defined streams. these streams are opened when C++
program is executed. The pre-defined streams are as follows :
Stream Name Used for Linked to
cin Standard input Keyboard
cout Standard output Monitor
cerr Standard error Monitor
clog Buffer error display Monitor

cin : it is an object of istream class. It is connected to standard input device


such as keyboard.
cout: it is an object of ostream class. It is connected to standard output device
such as monitor.
cerr: it is an object of ostream class. It is connected to standard error device.
The output through cerr is unbuffered. It is used to inform the user about
some error that has occurred.
clog: it is similar to cerr object but clog is buffered. It means that output will
be held in the buffer till the buffer becomes full or it is flushed.
Stream Class Hierarchy:
The stream classes are arranged in hierarchy. C++ provides the following classes to
perform output and input of characters with files.
1) ofstream: this stream class is used to write on files.
2) ifstream : this stream of class is used to read from files.
3) fstream : this stream class is used to both read and write
from/to files.

Opening Files:
A file should be opened before it can be processed. A file pointer is
declared and associated with the file to be opened.
Syntax:
Open(filename, mode)
Filename: it is the name of the file to be opened
Mode: it is the mode in which the file is to be opened. Its an optional
perameter.
Mode Description
ios::in Used to open a file for input operation
ios::out Used to open a file for output operation
ios::binary Used to open a file in binary mode
ios::nocreate Used to open a file only if it exists. The file is not created if it doesn’t exist.
ios::noreplace Used to open a file only if the file doesn’t exist otherwise open fails.

Test.open(“test.txt”,ios::out)

Default Opening Modes:


The default modes of classes are as follows

Class Default Mode Parameter


ofstream ios::out
ifstream ios::in
fstream ios::out | ios::in
Verifying File Open
The function is_open () is used to check if a stream object has opened a file
successfully. The function has no parameters and returns a value of true if
the file is open. It returns false if the file is not opened.

if (!Test)
cout<<“Error in opening the file”;

Closing Files
The function close () of stream is used to close a file. It takes no parameters.
The function is used as follows:
Test.close ()

Formatted Files I/O


The formatted I/O stores data on disk as a series of characters. Each digit or
character takes one byte on the disk.
Writing Data to Formatted Files I/O
The insertion operator << is used with cout object. It is frequently used in
programming to write the output on the screen. The same operator can also be used
to write data in files. The operator is used with a stream class of ofstream or fstream to
write data to files.

Example 1:
Write a program that writes three characters in a file using formatted I/O.
Ans:
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
#include<fstream.h>
void main ()
{ clrscr ();
int n = 10;
char ch = '*';
double d = 38.125;
ofstream file("c:\\test.txt");
file<<n<<' '<<ch<<' '<<d;
file.close();
getch();
}
Example 2:
Write a program that inputs the names of the five cities and stores them in a
file city.txt
Ans:
#include<iostream.h>
#include<conio.h>
#include<fstream.h>
void main ()
{ clrscr ();
int i;
char city [50];
ofstream file("c:\\city.txt");
for (i=0; i<5; i++)
{ cout<<"Enter the name of any city:";
cin>>city;
file<<city<<'\n'; }
file.close();
getch();
}
Reading Data from Formatted Files I/O
The extractio operator >> is used with cin object. It is frequently used in programming to read
the input from the keyboard. The same operator can also be used to read data from files. The
operator is used with a stream class of ifstream or fstream to read data from files.

Example 3:
Write a program that reads the contents of the file test.txt and display on screen.
Ans:
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
#include<fstream.h>
void main ()
{ clrscr ();
int n ;
char ch ;
double d ;
ifstream file("c:\\test.txt");
file>>n>>ch>>d;
cout<<" The contents of the file are as follows: "<<endl;
cout<<n<<endl<<ch<<endl<<d<<endl;
file.close();
getch();
}
Character I/O
The data can also be written to files character by character. Similarly the contents of a
file can be read character by character.
The put () function is used to write single character in a file.
The get () function is used to read single character from a file.

Writing Single Character


Syntax:
obj.put(ch)
obj: it’s the name of stream object that is associated with a file.
put: it’s the name of a function used to write a character in file.
ch : it’s the character to written in the file. It can be a constant or variable.

Reading Single Character


Syntax:
obj.get(ch)
obj: it’s the name of stream object that is associated with a file.
get: it’s the name of a function used to read a character in file.
ch : it’s the character variable used to store the character read from the file.
Example 4:
Write a program that inputs five characters from the user and stores them in a
file.
Ans:
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<fstream.h>
void main ()
{ clrscr ();
char ch;
ofstream out("c:\\chars.txt");
for (int i=0; i<5; i++)
{ cout<<"Enter a character:";
cin>>ch;
cout.put(ch); }
out.close();
getch();
}
Example 5:
Write a program that reads the characters from a text file and display them on
screen.
Ans:
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<fstream.h>
void main ()
{ clrscr ();
char ch;
ifstream in("c:\\chars.txt");
while (!in.eof())
{ in.get(ch);
cout<<ch<<endl; }
in.close();
getch();
}
Binary I/O
Binary I/O is used to store data in files in binary format. Its an efficient method to store a
large amount of data as compared to formatted I/O. for example, the formatted I/O
takes 4 bytes to store 1234 while binary I/O takes 2 bytes to store same data.

Writing Data in Binary I/O


The data can be stored in binary I/O using write () function of any object of
ofstream or fstream. The syntax of this function is as follows
Syntax:
obj.write((char*)&r, sizeof(r));
obj: it’s the name of stream object that is associated with a file.
write: it’s the name of a function used to write data with binary I/O in file.
char* : it’s the address of character data in fixed length bytes.
r : its the name of variable that contains data to be stored in the file.
sizeof(r): it calculates the size of r to determine the number of bytes required
to store data.
Reading Data in Binary I/O
The data can be read in binary I/O using read () function of any object
of ifstream or fstream. The syntax of this function is as follows
Syntax:
obj.read((char*)&r, sizeof(r));
obj: it’s the name of stream object that is associated with a file.
write: it’s the name of a function used to read data with binary I/O.
char* : it’s the address of character data in fixed length bytes.
r : its the name of variable that stores data read from the file.
sizeof(r): it calculates the size of r to determine the number of bytes
required to store data.

Assignment :
1. Example on Read Data in binary I/O
2. Example on Writing Data in binary I/O

You might also like