BINARY
FILES:
Binary
files are the file streams opened in the mode ios::binary. They perform input and output operations
independently of any format considerations. There is no need to format any
data, and data may not use the separation codes (like space, newline, etc…)
used by text files, to separate elements. Hence, in binary files, input and
output data with the extraction and insertion operators (<< and >>)
and functions like getline are not efficient.
Binary files are used to
store programs and data. Binary files can be classified in the way they are
accessed – Sequential access files
and Direct (or random) access files.
To input and output binary data sequentially, file streams contain two member
functions write() and read(). The function write() is a member function of ostream
inherited by ofstream and read() is
a member function of istream that is inherited by ifstream. Objects of class
fstream contain both the functions. Their prototypes are:
write(memory_block, size);
read(memory_block, size);
Where memory block is
of type “pointer to char” (char*), and represents the address of an array of
bytes where the read data elements are stored from where the data elements to
be written are taken. The size parameter
is an integer value that specifies the number of characters to be read form the
memory block or the number of characters to be written to the memory block. One
can also use these functions to write and read class objects in a file.
CLASSES
AND FILE OPERATIONS:
In order to write an object obj of a class into a file the
following format is used.
mfile.write( (char*)
&obj,sizeof(obj) );
where mfile is a
fstream object.
Similarly, in order
read an object obj of a class from a
file the following format is used.
mfile.write( (char*)
&obj,sizeof(obj) );
Program: This program
illustrates writing class objects into a file and reading the same.
#include<iostream.h>
#include<fstream.h>
#include<iomanip.h>
#include<conio.h>
class studentinfo
{
protected:
char name[20],sex;
int age;
float height,weight;
public:
void getdata();
void display();
};
void studentinfo : : getdata()
{
cout<<”enter the following information: \n”;
cout<<”name:”; cin>>name;
cout<<”Age:”; cin>>age;
cout<<”Sex:”; cin>>sex;
cout<<”height:”; cin>>height;
cout<<”weight:”; cin>>weight;
}
void studentinfo : : display()
{
cout<<name<<setw(5)<<age<<<<endl;
cout<<sex<<setw(5)<<height<<setw(5)<<weight<<endl;
}
void main()
{
clrscr();
studentinfo obj;
fstream mfile;
char fname[10];
cout<<”enter a file name: \n:”;
cin>>fname;
obj.getdata(); //reading an object from the
keyboard
mfile.open(fname, ios : : out);
cout<<”writing data into the
file……….\n”
mfile.write( (char*) &obj ,
sizeof(obj) ); //writing the object into the file
mfile.close();
mfile.open(fname, ios : : in);
cout<<”reading data from the
file………..\n”;
mfile.read( (char*) & obj,
sizeof(obj) ); //reading the object from the file
obj.display();
mfile.close();
getch();
}
The above program
contains a class studentinfo with
two functions getdata() and display(). An object obj of type studentinfo is created and the data in the object, received from
the keyboard, is written in a file opened in output mode ios : : out, the name of the file being chosen at runtime.
The file is then closed and opened
again in the input mode ios : : in. The data in the object obj stored earlier is then read from the file and displayed.
0 comments:
Post a Comment