File Handling
File Handling
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
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)
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 ()
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.
Assignment :
1. Example on Read Data in binary I/O
2. Example on Writing Data in binary I/O