2. Introduction
The only difference is that while working with binary files, you do
need to prefix file modes with ios::binary at the time of opening the
file.
You have learnt how to create files (open them in ios::out or ios::app
mode which will create a file if it doesn't exist already),
To read records from binary files (call read() function through
stream object to which file is attached),
To write records in binary files (call write() function through stream
object which file is attached).
3. BASIC OPERATIONS IN A BINARY FILE
Searching
Appending data
Inserting data in sorted file
Modifying data
Deleting a record
4. Searching in a file
We can perform search in a binary file opened in input mode by
reading each record then checking whether it is our desired record
or not.
For instance, if you want to search for a record for a student having
rollno as 1 in file marks.dat, you can implement this search process in
C++ in two manners :
with records implemented through structures.
with records implemented through classes.
6. //Example : To search a record in a file
void main()
{
clrscr();
fstream fio("marks.dat", ios::in | ios::out);
char ans='y';
while(ans=='y' || ans=='Y')
{
stud1.getdata();
fio.write((char *)&stud1, sizeof(stud1));
cout<<"Record added to the filen";
cout<<"nWant to enter more ? (y/n)..";
cin>>ans;
}
clrscr();
int rno;
long pos;
char found='f';
cout<<"Enter rollno of student to be search for: ";
cin>>rno;
fio.seekg(0);
while(!fio.eof())
{
pos=fio.tellg();
fio.read((char *)&stud1, sizeof(stud1));
if(stud1.getrno() == rno)
{
stud1.putdata();
fio.seekg(pos);
found='t';
break;
}
}
if(found=='f')
{
cout<<"nRecord not found in the file..!!n";
cout<<"Press any key to exit...n";
getch();
exit(2);
}
fio.close();
getch();
}
8. Appending Data in C++
To append data in a file, the file is opened with the
following two specifications :
file is opened in output mode
file is opened in ios::app mode
Once the file gets opened in ios::app mode, the previous
records/information is retained and new data gets
appended to the file.
9. // Example : to append data in a file in C++
class student
{
:// class definition
} stud1;
void main()
{
clrscr();
ofstream fout("marks.dat", ios::app);
char ans='y';
while(ans=='y' || ans=='Y')
{
stud1.getdata();
fout.write((char *)&stud1, sizeof(stud1));
cout<<"Data appended in the file successfully..!!n";
cout<<"nWant to enter more ? (y/n)..";
cin>>ans;
}
fout.close();
cout<<"nPress any key to exit...n";
getch();
}
11. Inserting Data in Sorted File
To insert data in a sorted file, firstly, its appropriated position
is determined
and then records in the file prior to this determined position
are copied to temporary file,
followed by the new record to be inserted and then the rest
of the records from the file are also copied.
12. Inserting Data in Sorted File
STEPS :
(Open the file in read mode(marks.dat) and open temp.dat in write mode)
i) Determining the appropriate position.
(ii) Copy the records prior to determined position to a temporary file say
temp.dat.
(iii) Append the new record in the temporary file temp.dat.
(iv) Now append the rest of the records in temporary file temp.dat.
(v) Delete the file marks.dat by using the following code.
remove("marks.dat");
(vi) Now, rename the file temp.dat as marks.dat as follows :
rename("temp.dat", "marks.dat");
13. //EXAMPLE to insert a new record in a sorted file
class student
{
: // class definition
} stud1;
void main()
{
clrscr();
ifstream fin("marks.dat", ios::in);
ofstream fout("temp.dat", ios::out);
char last='y';
cout<<"Enter details of student whose record is
to be insertedn";
stud1.getdata();
while(!fin.eof())
{
fin.read((char *)&stud, sizeof(stud));
if(stud1.getrno()<=stud.getrno())
{
fout.write((char *)&stud1,
sizeof(stud1));
last = 'n';
break; }
else
{
fout.write((char *)&stud, sizeof(stud));
} }
if(last == 'y')
{ fout.write((char *)&stud1, sizeof(stud1)); }
else if(!fin.eof())
{
while(!fin.eof())
{
fin.read((char *)&stud, sizeof(stud));
fout.write((char *)&stud, sizeof(stud));
} }
fin.close();
fout.close();
remove("marks.dat");
rename("temp.dat", "marks.dat");
fin.open("marks.dat", ios::in);
cout<<"File now contains:n";
while(!fin.eof())
{ fin.read((char *)&stud, sizeof(stud));
if(fin.eof())
{
break;
}
stud.putdata();
}
fin.close();
getch();
}
15. Deleting a record from a File
STEPS :
To delete a record, following procedure is carried out :
(i) Firstly, determine the position of the record to be deleted, by performing
a search in the file.
(ii) Keep copying the records other than the record to be deleted in a
temporary file say temp.dat.
(iii) Do not copy the record to be deleted to temporary file, temp.dat.
(iv) Copy rest of the records to temp.dat.
(v) Delete original file say marks.dat as :
remove("marks.dat");
(vi) Rename temp.dat as marks.dat as :
rename("temp.dat", "marks.dat");
16. //EXAMPLE to delete a record from a file
class student
{
: // class definition
} stud1;
void main()
{
clrscr();
ifstream fio(“marks.dat",ios::in);
ofstream file("temp.dat",ios::out|ios::app);
int rno;
char found='f',confirm='n';
cout<<"enter rollno of student whose record is to be deleted n";
cin>>rno;
while(!fio.eof())
{
fio.read((char*)&stud1,sizeof(stud1));
if(stud1.getrno()==rno)
{
stud1.putdata();
found=‘t';
cout<<"are you sure want to delete this record(y/n)";
cin>>confirm;
if(confirm=='n')// if confirm is y then record is not written
file.write((char*)&stud1,sizeof(s1));
}
else
file.write((char*)&stud1,sizeof(stud1));
}
if(found=='f')
cout<<"record not found!!n";
fio.close();
file.close();
remove(“marks.dat");
rename("temp.dat",“marks.dat");
fio.open(“marks.dat",ios::in);
cout<<"now file contains n";
while(!fio.eof())
{
fio.read((char*)&stud1,sizeof(stud1));
if(fio.eof())
break;
stud1.putdata();
}
fio.close();
getch();
}
17. Modifying a record in a File
STEPS :
To modify a record,
the file is opened in I/O mode
and an important step is performed that gives the beginning address
of record being modified.
After the record is modified in memory, the file pointer is once again
placed at the beginning position of this record and then record is
rewritten.
18. //EXAMPLE to insert a new record in a sorted file
class student
{
:
void modify();
}stud1;
void main( )
{
clrscr();
fstream fio("marks.dat", ios::in | ios::out); /* Read rollno whose data is to be modified */
long pos;
while(!fio.eof())
{
pos = fio.tellg() // determine the beginning position of record
fio.read((char *) & stud1, sizeof(stud1));
if(stud1.getrno() == rollno) // this is the record to be modified
{
stud1.modify(); // get the new data
fio.seekg(pos); // place file pointer at the beginning record position
fio.write((char *) & stud1, sizeof(stud1)); // now write the modified record
}
}
}