You are on page 1of 6

#include<iostream>

#include<fstream>
#include<string.h>
using namespace std;
class student
{
unsigned short int roll;
char name[30];
float percent;
public: void getdata()
{ cout<<"\nEnter the info :"; cin>>roll>>name>>percent;}
void putdata()
{cout<<"\n"<<roll<<" "<<name<<" "<<percent;}
int check( char *p ) { if (!strcmp(name,p) ) return 1; else return 0;}
int check1( char p ) { if (name[0]==p ) return 1; else return 0;}
};

void creation();
void display();
void cdisplay();
void append();
void split();
void merge();
void modify();
void insertion();
void deletion();

int main()
{
int c;
do
{
cout<<"\n 1: creation 2: display 3: conditional display 4: append 5:split
6:merge 7:modify 8:insertion 9:deletion 0:exit";
cout<<"\n enter the choice :";
cin>>c;
switch(c)
{
case 1: creation(); break;
case 2: display(); break;
case 3: cdisplay(); break;
case 4: append(); break;
case 5: split(); break;
case 6: merge(); break;
case 7: modify(); break;
case 8: insertion(); break;
case 9:deletion(); break;
}
}while(c);

return 0;
}

void creation()
{
fstream a;
student z;
int i, n;
cout<<"\nEnter the total number of records to be entered:";
cin>>n;
a.open("sample.dat",ios::out|ios::binary);
for(i=1;i<=n;i++)
{
z.getdata();
a.write( (char*)&z , sizeof(z)); // writing to a binary file
}
a.close();
}

void display()
{
fstream a;
student z;
char s[30];
cout<<"\nEnter the filename to be displayed: ";
cin>>s;
a.open(s,ios::in|ios::binary); // open a binary file

while(a.read( (char*)&z , sizeof(z))) // reading an object from a binary file


z.putdata();

a.close();
}

void cdisplay()
{
fstream a;
student z;
char t[30];
int s=0;
a.open("sample.dat",ios::in|ios::binary); // open a binary file
cout<<"\nEnter the name to be searched :";
cin>>t;
while(a.read( (char*)&z , sizeof(z))) // reading an object from a binary file
if( z.check(t)) { s=1; break;}
if(s) cout<<"\nThe student has a record in our file"; else cout<<"\nStudent record
doesnt exists";
a.close();
}

void append()
{
student z;
fstream a;
a.open("sample.dat",ios::out|ios::binary| ios::app); // open a binary file
cout<<"\n Enter the new student's info to be appended :";
z.getdata();
a.write( (char*)&z , sizeof(z));
a.close();
}

void split()
{
student z;
fstream a,b,c;
a.open("sample.dat",ios::binary|ios::in);
b.open("first.dat",ios::binary|ios::out);
c.open("second.dat",ios::binary|ios::out);

while(a.read( (char*)&z , sizeof(z)))


{
if(z.check1('a')) b.write( (char*)&z , sizeof(z));
if(z.check1('s')) c.write( (char*)&z , sizeof(z));
}
a.close();
b.close();
c.close();
}

void merge()
{
student z;
fstream a,b,c;
a.open("sample.dat",ios::binary|ios::out);
b.open("first.dat",ios::binary|ios::in);
c.open("second.dat",ios::binary|ios::in);

while(b.read( (char*)&z , sizeof(z)))


a.write( (char*)&z , sizeof(z));

while(c.read( (char*)&z , sizeof(z)))


a.write( (char*)&z , sizeof(z));

a.close();
b.close();
c.close();
}

void modify()
{
unsigned short int r,c=1;
fstream a,b;
student x,z;
a.open("sample.dat",ios::binary|ios::in);
b.open("temp.dat",ios::binary|ios::out);

cout<<"\n Enter the record no. to be modified : ";


cin>>r;
cout<<"\n Enter the new record...";
x.getdata();

while(a.read( (char*)&z , sizeof(z)))


{
if(c==r) b.write( (char*)&x , sizeof(x)); else b.write( (char*)&z ,
sizeof(z));
c++;
}

a.close();
b.close();
remove("sample.dat");
rename("temp.dat","sample.dat");
}

void insertion()
{
unsigned short int pos,c=1;
fstream a,b;
student x,z;
a.open("sample.dat",ios::binary|ios::in);
b.open("temp.dat",ios::binary|ios::out);

cout<<"\n Enter the record no. to be inserted : ";


x.getdata();
cout<<"\n Enter the position : ";
cin>>pos;

while(a.read( (char*)&z , sizeof(z)))


{
if(c==pos) b.write( (char*)&x , sizeof(x));
b.write( (char*)&z , sizeof(z));
c++;
}

a.close();
b.close();
remove("sample.dat");
rename("temp.dat","sample.dat");
}

void deletion()
{
unsigned short int pos,c=1;
fstream a,b;
student z;
a.open("sample.dat",ios::binary|ios::in);
b.open("temp.dat",ios::binary|ios::out);

cout<<"\n Enter the position of record to be deleted: ";


cin>>pos;

while(a.read( (char*)&z , sizeof(z)))


{
if(c!=pos) b.write( (char*)&z , sizeof(z));
c++;
}

a.close();
b.close();
remove("sample.dat");
rename("temp.dat","sample.dat");
}

You might also like