Showing posts with label File Handling. Show all posts
Showing posts with label File Handling. Show all posts

Saturday, June 22, 2013

SEQUENTIAL FILE ORGANISATION

SEQUENTIAL FILE ORGANISATION


Following code explains the concept of sequential access file:


/*************************************************************************************
Program for performing various operations on Sequential File organisation.
**************************************************************************************/
#include<iostream.h>
#include<iomanip.h>
#include<fstream.h>
#include<string.h>
#include<conio.h>
#include<stdlib.h>
class EMP_CLASS
{
 typedef struct EMPLOYEE
 {
char name[10];
int emp_id;
int salary;
 }Rec;
 Rec Records;
public:
void Create();
void Display();
void Update();
void Delete();
void Append();
int Search();
};
void EMP_CLASS::Create()
{
char ch='y';
fstream seqfile;
seqfile.open("EMP.DAT",ios::in|ios::out|ios::binary);
do
{
cout<<"\n Enter Name: ";
cin>>Records.name;
cout<<"\n Enter Emp_ID: ";
cin>>Records.emp_id;
cout<<"\n Enter Salary: ";
cin>>Records.salary;
//then write the record containing this data in the file
seqfile.write((char*)&Records,sizeof(Records));
cout<<"\nDo you want to add more records?";
cin>>ch;
}while(ch=='y');
seqfile.close();
}
void EMP_CLASS::Display()
{
fstream seqfile;
int n,m,i;
seqfile.open("EMP.DAT",ios::in|ios::out|ios::binary);
//positioning the pointer in the file at the beginning
seqfile.seekg(0,ios::beg);
cout<<"\n The Contents of file are ..."<<endl;
//read the records sequentially
while(seqfile.read((char *)&Records,sizeof(Records)))
{
if(Records.emp_id!=-1)
{
cout<<"\nName: "<<Records.name;
cout<<"\nEmp_ID: "<<Records.emp_id;
cout<<"\nSalary: "<<Records.salary;
cout<<"\n";
}
 }
 int last_rec=seqfile.tellg();//last record position
//formula for computing total number of  objects in the file
 n=last_rec/(sizeof(Rec));
 cout<<"\n\n Total number of objects are "<<n<<"(considering logical deletion)";
 seqfile.close();
}
void EMP_CLASS::Update()
{
int pos;
cout<<"\n For updation,";
fstream seqfile;
seqfile.open("EMP.DAT",ios::in|ios::out|ios::binary);
seqfile.seekg(0,ios::beg);
//obtaining the position of desired record in the file
pos=Search();
if(pos==-1)
{
cout<<"\n The record is not present in the file";
return;
}
//calculate the actual offset of the desired record in the file
int offset=pos*sizeof(Rec);
seqfile.seekp(offset);//seeking the desired record for modification
cout<<"\n Enter the values for updation...";
cout<<"\n Name: ";cin>>Records.name;
cout<<"\n Emp_Id: ";cin>>Records.emp_id;
cout<<"\n Salary: ";cin>>Records.salary;
seqfile.write((char*)&Records,sizeof(Records))<<flush;
seqfile.seekg(0);
seqfile.close();
cout<<"\n The record is updated!!!";
}
void EMP_CLASS::Delete()
{
int id,pos;
cout<<"\n For deletion,";
fstream seqfile;
seqfile.open("EMP.DAT",ios::in|ios::out|ios::binary);
seqfile.seekg(0,ios::beg);//seeking for reading purpose
pos=Search();//finding pos. for the record to be deleted
if(pos==-1)
{
cout<<"\n The record is not present in the file";
return;
}
//calculate offset to locate the desired record in the file
int offset=pos*sizeof(Rec);
seqfile.seekp(offset);//seeking the desired record for deletion
strcpy(Records.name,"");
Records.emp_id=-1;
Records.salary=-1;
seqfile.write((char*)&Records,sizeof(Records))<<flush;
seqfile.seekg(0);
seqfile.close();
cout<<"\n The record is Deleted!!!";
}
void EMP_CLASS::Append()
{
fstream seqfile;
seqfile.open("EMP.DAT",ios::ate|ios::in|ios::out|ios::binary);
seqfile.seekg(0,ios::beg);
int i=0;
while(seqfile.read((char *)&Records,sizeof(Records)))
{
i++;//going through all the records
// for reaching at the end of the file
}
//instead of above while loop
//we can also use seqfile.seekg(0,ios::end)
//for reaching at the end of the file
seqfile.clear();//turning off EOF flag
cout<<"\n Enter the record for appending";
cout<<"\nName: ";cin>>Records.name;
cout<<"\nEmp_ID: ";cin>>Records.emp_id;
cout<<"\nSalary: ";cin>>Records.salary;
seqfile.write((char*)&Records,sizeof(Records));
seqfile.seekg(0);//reposition to start(optional)
seqfile.close();
cout<<"\n The record is Appended!!!";
}
int EMP_CLASS::Search()
{
fstream seqfile;
int id,pos;
cout<<"\n Enter the Emp_ID for searching the record ";
cin>>id;
seqfile.open("EMP.DAT",ios::ate|ios::in|ios::out|ios::binary);
seqfile.seekg(0,ios::beg);
pos=-1;
int i=0;
while(seqfile.read((char *)&Records,sizeof(Records)))
{
if(id==Records.emp_id)
{
pos=i;
break;
}
i++;
}
return pos;
}
void main()
{
EMP_CLASS List;
char ans='y';
int choice,key;
clrscr();
do
{
cout<<"\n             Main Menu             "<<endl;
cout<<"\n 1.Create";
cout<<"\n 2.Display";
cout<<"\n 3.Update";
cout<<"\n 4.Delete";
cout<<"\n 5.Append";
cout<<"\n 6.Search";
cout<<"\n 7.Exit";
cout<<"\n Enter your choice ";
cin>>choice;
switch(choice)
{
case 1:List.Create();
break;
case 2:List.Display();
break;
case 3:List.Update();
break;
case 4:List.Delete();
break;
case 5:List.Append();
break;
case 6:key=List.Search();
if(key<0)
cout<<"\n Record is not present in the file";
else
cout<<"\n Record is present in the file";
break;
case 7:exit(0);
}
cout<<"\n\t Do you want to go back to Main Menu?";
cin>>ans;
}while(ans=='y');
}

Please ask for explanation if you face any problem in understanding the code.It is better to understand than just copying and running a code.

INDEX SEQUENTIAL FILE

INDEX SEQUENTIAL FILE


Following code illustrates the concept of index sequential file:


/***********************************************************************
Program for performing various operations on
Index Sequential File organisation.
************************************************************************/
#include<iostream.h>
#include<iomanip.h>
#include<fstream.h>
#include<string.h>
#include<conio.h>
#include<stdlib.h>
class EMP_CLASS
{
 typedef struct EMPLOYEE
 {
char name[10];
int emp_id;
int salary;
 }Rec;
 typedef struct INDEX
 {
int emp_id;
int position;
 }Ind_Rec;
 Rec Records;
 Ind_Rec Ind_Records;
public:
EMP_CLASS();
void Create();
void Display();
void Update();
void Delete();
void Append();
void Search();
};
EMP_CLASS::EMP_CLASS()//constructor 
{
strcpy(Records.name,"");
}
void EMP_CLASS::Create()
{
int i,j;
char ch='y';
fstream seqfile;
fstream indexfile;
i=0;
indexfile.open("IND.DAT",ios::in|ios::out|ios::binary);
seqfile.open("EMP.DAT",ios::in|ios::out|ios::binary);
do
{
cout<<"\n Enter Name: ";
cin>>Records.name;
cout<<"\n Enter Emp_ID: ";
cin>>Records.emp_id;
cout<<"\n Enter Salary: ";
cin>>Records.salary;
seqfile.write((char*)&Records,sizeof(Records))<<flush;
Ind_Records.emp_id=Records.emp_id;
Ind_Records.position=i;
indexfile.write((char*)&Ind_Records,sizeof(Ind_Records))<<flush;
i++;
cout<<"\nDo you want to add more records?";
cin>>ch;
}while(ch=='y');
seqfile.close();
indexfile.close();
}
void EMP_CLASS::Display()
{
fstream seqfile;
fstream indexfile;
int n,i,j;
seqfile.open("EMP.DAT",ios::in|ios::out|ios::binary);
indexfile.open("IND.DAT",ios::in|ios::out|ios::binary);
indexfile.seekg(0,ios::beg);
seqfile.seekg(0,ios::beg);
cout<<"\n The Contents of file are ..."<<endl;
i=0;
while(indexfile.read((char *)&Ind_Records,sizeof(Ind_Records)))
{

i=Ind_Records.position*sizeof(Rec);//getting pos from index file
seqfile.seekg(i,ios::beg);//seeking record of that pos from seq.file
seqfile.read((char *)&Records,sizeof(Records));//reading record
if(Records.emp_id!=-1)//if rec. is not deleted logically
{   //then display it
cout<<"\nName: "<<Records.name<<flush;
cout<<"\nEmp_ID: "<<Records.emp_id;
cout<<"\nSalary: "<<Records.salary;
cout<<"\n";
}

}
 seqfile.close();
 indexfile.close();
}
void EMP_CLASS::Update()
{
int pos,id;
char New_name[10];
int New_emp_id;
int New_salary;
cout<<"\n For updation,";
cout<<"\n Enter the Emp_ID for for searching ";
cin>>id;
fstream seqfile;
fstream indexfile;
seqfile.open("EMP.DAT",ios::in|ios::out|ios::binary);
indexfile.open("IND.DAT",ios::in|ios::out|ios::binary);
indexfile.seekg(0,ios::beg);

pos=-1;
//reading index file for getting the index
while(indexfile.read((char *)&Ind_Records,sizeof(Ind_Records)))
{
if(id==Ind_Records.emp_id)//the desired record is found
{
pos=Ind_Records.position;//getting the position
break;
}
}
if(pos==-1)
{
cout<<"\n The record is not present in the file";
return;
}
else
{
cout<<"\n Enter the values for updation...";
cout<<"\n Name: ";cin>>New_name;
cout<<"\n Salary: ";cin>>New_salary;
//calculating the position of record in seq. file using the pos of ind. file
int offset=pos*sizeof(Rec);
seqfile.seekp(offset);//seeking the desired record for modification
strcpy(Records.name,New_name);//can be updated
Records.emp_id=id;//It's unique id,so don't change
Records.salary=New_salary;//can be updated
seqfile.write((char*)&Records,sizeof(Records))<<flush;
cout<<"\n The record is updated!!!";
}
seqfile.close();
indexfile.close();

}
void EMP_CLASS::Delete()
{
int id,pos;
cout<<"\n For deletion,";
cout<<"\n Enter the Emp_ID for for searching ";
cin>>id;
fstream seqfile;
fstream indexfile;
seqfile.open("EMP.DAT",ios::in|ios::out|ios::binary);
indexfile.open("IND.DAT",ios::in|ios::out|ios::binary);
seqfile.seekg(0,ios::beg);
indexfile.seekg(0,ios::beg);
pos=-1;
//reading index file for getting the index
while(indexfile.read((char *)&Ind_Records,sizeof(Ind_Records)))
{
if(id==Ind_Records.emp_id) //desired record is found
{
pos=Ind_Records.position;
Ind_Records.emp_id=-1;
break;
}
}
if(pos==-1)
{
cout<<"\n The record is not present in the file";
return;
}
//calculating the position of record in seq. file using the pos of ind. file
int offset=pos*sizeof(Rec);
seqfile.seekp(offset);//seeking the desired record for deletion
strcpy(Records.name,"");
Records.emp_id=-1; //logical deletion
Records.salary=-1; //logical deletion
seqfile.write((char*)&Records,sizeof(Records))<<flush;//writing deleted status 
    //From index file also the desired record gets deleted as follows
offset=pos*sizeof(Ind_Rec);//getting position in index file
indexfile.seekp(offset); //seeking that record
Ind_Records.emp_id=-1; //logical deletion of emp_id
Ind_Records.position=pos;//position remain unchanged
indexfile.write((char*)&Ind_Records,sizeof(Ind_Records))<<flush;
seqfile.seekg(0);
indexfile.close();
seqfile.close();
cout<<"\n The record is Deleted!!!";
}
void EMP_CLASS::Append()
{
fstream seqfile;
fstream indexfile;
int pos;
indexfile.open("IND.DAT",ios::in|ios::binary);
indexfile.seekg(0,ios::end);
pos=indexfile.tellg()/sizeof(Ind_Records);
indexfile.close();

indexfile.open("IND.DAT",ios::app|ios::binary);
seqfile.open("EMP.DAT",ios::app|ios::binary);

cout<<"\n Enter the record for appending";
cout<<"\nName: ";cin>>Records.name;
cout<<"\nEmp_ID: ";cin>>Records.emp_id;
cout<<"\nSalary: ";cin>>Records.salary;
seqfile.write((char*)&Records,sizeof(Records));//inserting rec at end in seq. file
Ind_Records.emp_id=Records.emp_id;           //inserting rec at end in ind. file
Ind_Records.position=pos;                          //at calculated pos
indexfile.write((char*)&Ind_Records,sizeof(Ind_Records))<<flush;
seqfile.close();
indexfile.close();
cout<<"\n The record is Appended!!!";
}
void EMP_CLASS::Search()
{
fstream seqfile;
fstream indexfile;
int id,pos,offset;
cout<<"\n Enter the Emp_ID for searching the record ";
cin>>id;
indexfile.open("IND.DAT",ios::in|ios::binary);
pos=-1;
//reading index file to obtain the index of desired record
while(indexfile.read((char *)&Ind_Records,sizeof(Ind_Records)))
{
if(id==Ind_Records.emp_id)//desired record found
{
pos=Ind_Records.position;//seeking the position
break;
}
}
if(pos==-1)
{
cout<<"\n Record is not present in the file";
return;
}
//calculate offset using position obtained from ind. file
offset=pos*sizeof(Records);
seqfile.open("EMP.DAT",ios::in|ios::binary);
//seeking the record from seq. file using calculated offset
seqfile.seekg(offset,ios::beg);//seeking for reading purpose
seqfile.read((char *)&Records,sizeof(Records));
if(Records.emp_id==-1)
{
cout<<"\n Record is not present in the file";
return;
}
else //emp_id=desired record’s id
{
cout<<"\n The Record is present in the file and it is...";
cout<<"\n Name: "<<Records.name;
cout<<"\n Emp_ID: "<<Records.emp_id;
cout<<"\n Salary: "<<Records.salary;
}
seqfile.close();
indexfile.close();
}
void main()
{
EMP_CLASS List;
char ans='y';
int choice,key;
clrscr();
do
{
cout<<"\n             Main Menu             "<<endl;
cout<<"\n 1.Create";
cout<<"\n 2.Display";
cout<<"\n 3.Update";
cout<<"\n 4.Delete";
cout<<"\n 5.Append";
cout<<"\n 6.Search";
cout<<"\n 7.Exit";
cout<<"\n Enter your choice: ";
cin>>choice;
switch(choice)
{
case 1:List.Create();
break;
case 2:List.Display();
break;
case 3:List.Update();
break;
case 4:List.Delete();
break;
case 5:List.Append();
break;
case 6:List.Search();
break;
case 7:exit(0);
}
cout<<"\n\t Do you want to go back to Main Menu?";
cin>>ans;
}while(ans=='y');
}


Please feel free to ask if you face any problem in understanding the code.

DIRECT ACCESS FILE

DIRECT ACCESS FILE


The following code shows the implementation of direct access file:

/***********************************************************************
Program for implementing Direct Access File using the hashing technique.
Collision handling is performed using linear probing and
chaining without replacement.
 [hash function=(record_id mod 10)]
************************************************************************/
#include<iostream.h>
#include<iomanip.h>
#include<fstream.h>
#include<string.h>
#include<conio.h>
#include<stdlib.h>
class EMP_CLASS
{
 typedef struct EMPLOYEE
 {
char name[10];
int emp_id;
int salary;
int link;
int loc;
 }Rec;
 Rec Records;
public:
int size;
int Chain_tab[10][10];
EMP_CLASS();
void Insert();
void init();
void Display();
void Search();
void set_chain();
friend int Hash(int);
};
EMP_CLASS::EMP_CLASS()
{
strcpy(Records.name,"");
Records.emp_id=-1;
Records.salary=-1;
Records.link=-1;
}
void EMP_CLASS::init()
{
 fstream seqfile;
 seqfile.open("EMP.DAT",ios::out|ios::binary);
 cout<<"\n Enter the Hash table size ";
 cin>>size;
 for(int i=0;i<size;i++)
 {
  strcpy(Records.name,"");
  Records.emp_id=-1;
  Records.salary=-1;
  Records.link=-1;
  Records.link=i;
seqfile.write((char*)&Records,sizeof(Records));
  for(int i=0;i<size;i++)
  for(int j=0;j<size;j++)
  Chain_tab[i][j]=-1;
 }
 cout<<"\n\n Hash table is initialised...";
 cout<<"\n Now, insert the records in the hash table";
 seqfile.close();
}
int Hash(int num)
{
 int key;
 key=num%10;
 return key;
}
void EMP_CLASS::set_chain()
{
 fstream seqfile;
 int i,j,h,offset;
 seqfile.open("EMP.DAT",ios::in|ios::out|ios::binary);
 for(i=0;i<size;i++)
{
h=i;
for(j=0;j<size;j++)
{
if(Chain_tab[i][j]==1)
{
offset=h*sizeof(Records);
seqfile.seekg(offset);
seqfile.read((char*)&Records,sizeof(Records));
seqfile.seekp(offset);
Records.link=j;
seqfile.write((char*)&Records,sizeof(Records));
h=j;
}
}
}
seqfile.close();
}
void EMP_CLASS::Insert()
{
int i,h;
char ch='y';
char new_name[10];
int new_emp_id;
int new_salary;
fstream seqfile;
init();//initialising the hash table
seqfile.open("EMP.DAT",ios::in|ios::out|ios::binary);
do
{
cout<<"\n Enter Name: ";
cin>>new_name;
cout<<"\n Enter Emp_ID: ";
cin>>new_emp_id;
cout<<"\n Enter Salary: ";
cin>>new_salary;
h=Hash(new_emp_id);
int offset;
offset=h*sizeof(Records);

//seeking for reading record
seqfile.seekg(offset);
seqfile.read((char*)&Records,sizeof(Records));
//seeking for writing record
seqfile.seekp(offset);
if(Records.emp_id==-1)
{
 strcpy(Records.name,new_name);
 Records.emp_id=new_emp_id;
 Records.salary=new_salary;
 Records.link=-1;
 Records.loc=h;//h is used for marking the loc.
 seqfile.write((char*)&Records,sizeof(Records))<<flush;
//thus rec. is inserted at the hashed position in file
}
else//collision occurs
{
int flag=0;
int prev_link=Records.loc;
do      //handling collision
{
h++;//searching down for empty loc.in the file
if(h>size+1)
{
cout<<"\n The hash table is Full, Can't insert record!!!";
return;

}
offset=h*sizeof(Records);
seqfile.seekg(offset);
seqfile.read((char*)&Records,sizeof(Records));
if(Records.emp_id==-1) //finding empty loc. using linear probing
{
seqfile.seekp(offset);//seeking the empty slot in the file
strcpy(Records.name,new_name);//for placing the record
Records.emp_id=new_emp_id;
Records.salary=new_salary;
Records.link=-1;
Records.loc=h;//setting the location for colliding record
seqfile.write((char*)&Records,sizeof(Records))<<flush; 
//collinding record is placed in the file at proper pos.
//chain table is maintained for keeping track of all the colliding entries.
Chain_tab[prev_link][h]=1;
flag=1;//indicates colliding record is inserted
}//end of if
}while(flag==0);//collision handled
}//end of else
cout<<"\nDo you want to add more records?";
cin>>ch;
set_chain();//setting the chain to handle collision
}while(ch=='y');
seqfile.close();
}
void EMP_CLASS::Display()
{
fstream seqfile;
seqfile.open("EMP.DAT",ios::in|ios::out|ios::binary);
seqfile.seekg(0,ios::beg);
cout<<"\n The Contents of file are ..."<<endl;
cout<<"\nLoc.      Name         Emp_ID      Salary        Link    ";
while(seqfile.read((char *)&Records,sizeof(Records)))
{

if(strcmp(Records.name,"")!=0)//not displaying empty slots
{
cout<<"\n--------------------------------------------------------\n";
cout<<Records.loc<<"       "<<Records.name<<flush<<"      "<<Records.emp_id;
cout<<"          "<<Records.salary<<"          "<<Records.link;

      }
}
 seqfile.close();
}
void EMP_CLASS::Search()
{
fstream seqfile;
int key,h,offset,flag=0;
cout<<"\n Enter the Emp_ID for searching the record ";
cin>>key;
seqfile.open("EMP.DAT",ios::in|ios::binary);
h=Hash(key);//obtaining the location of rec.using hash function
while(seqfile.eof()==0)
{       //h is a hash key
offset=h*sizeof(Records);
//using h for getting actual position in the file
//hence offset is calculated
seqfile.seekg(offset,ios::beg);//seeking rec.of that offset
seqfile.read((char *)&Records,sizeof(Records));//reading that rec.
if(key==Records.emp_id)//checking if it is required rec.
{
cout<<"\n The Record is present in the file and it is...";
cout<<"\n Name: "<<Records.name;
cout<<"\n Emp_ID: "<<Records.emp_id;
cout<<"\n Salary: "<<Records.salary;
flag=1;//means desired reocrd is obtained
return;
}
else//following link for colliding record
{
h=Records.link;//moving along the chain
}
}//endof while
if(flag==0)
cout<<"\n The Record is not present in the file";
seqfile.close();
}
void main()
{
EMP_CLASS List;
char ans='y';
int choice,key;
clrscr();
do
{
cout<<"\n             Main Menu             "<<endl;
cout<<"\n 1.Insert";
cout<<"\n 2.Display";
cout<<"\n 3.Search";
cout<<"\n 4.Exit";
cout<<"\n Enter your choice: ";
cin>>choice;
switch(choice)
{
case 1:List.Insert();
break;
case 2:List.Display();
break;
case 3:List.Search();
break;
case 4:exit(0);
}
cout<<"\n\t Do you want to go back to Main Menu?";
cin>>ans;
}while(ans=='y');
}