You are on page 1of 7

VARUN VARSHNEY

06996402712
EXPERIMENT -2
WRITE A PROGRAM TO DIFFERENTIATE BETWEEN FILE SYSTEM AND
DATABASE MANAGEMENT SYSTEM
#include<fstream.h>
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<ctype.h>
#include<stdlib.h>
class Student
{
int SID;
char City[10],FName[20],LName[20];
public: void getStudent()
{
cout<<"Enter student id :- ";
cin>>SID;
cout<<"Enter first name:- " ;
gets(FName);
cout<<"Enter last name:- ";
gets(LName);
cout<<"Enter City:- ";
gets(City);
}
int getSID()
{
return SID;
}
void putStudent()
{
cout<<"Student ID:- "<<SID<<endl;
cout<<"First Name:- ";
puts(FName);
cout<<"Last Name:- ";
puts(LName);
cout<<"City:- ";
puts(City);
cout<<endl;
}
};
void viewRecords()
{
fstream F;
Student S;

VARUN VARSHNEY
06996402712
F.open("STUDENT.DAT",ios::binary|ios::in);
while(F.read((char*)&S,sizeof(S)))
{
S.putStudent();
}
F.close();
}
void insertRec()
{
fstream F;
Student S;
F.open("STUDENT.DAT",ios::binary|ios::out);
char ch;
do
{
S.getStudent();
F.write((char*)&S,sizeof(S));
cout<<"Enter more records?? (Y/N): ";
cin>>ch;
cout<<endl;
}
while(toupper(ch)=='Y');
F.close();
}
void updateRec()
{
fstream F;
Student S;
int SID,FOUND;
F.open("STUDENT.DAT",ios::in|ios::out|ios::binary);
cout<<"Enter student id to update:- ";
cin>>SID;
while(F.read((char*)&S,sizeof(S)))
if(S.getSID()==SID)
{
FOUND++;
F.seekg(0,ios::cur);
cout<<"Enter new details:- "<<endl;
S.getStudent();
F.seekp(F.tellg()-sizeof(S));
F.write((char*)&S,sizeof(S));
}
F.close();
}
void deleteRec()

VARUN VARSHNEY
06996402712
{
int SID;
cout<<"Enter student id to delete:- ";
cin>>SID;
fstream F1,F2;
Student S;
F1.open("STUDENT.DAT",ios::binary|ios::in);
F2.open("TempSTUD.DAT",ios::binary|ios::out);
int Del=0;
while(F1.read((char*)&S,sizeof(S)))
if(S.getSID()!=SID)
F2.write((char*)&S,sizeof(S));
else
Del++;
if(!Del)
cout<<"Record match not found XX"<<endl;
else
cout<<"Record deleted"<<endl;
F1.close();
F2.close();
F1.open("STUDENT.DAT",ios::binary|ios::out);
F2.open("TempSTUD.DAT",ios::binary|ios::in);
while(F2.read((char*)&S,sizeof(S)))
F1.write((char*)&S,sizeof(S));
F1.close();
F2.close();
}
void selectiveView()
{
fstream F;
Student S;
F.open("STUDENT.DAT",ios::binary|ios::in);
int chSID;
cout<<"Enter student id to view record:- ";
cin>>chSID;
while(F.read((char*)&S,sizeof(S)))
{
if(S.getSID()==chSID)
S.putStudent();
}
F.close();
}
void main()
{
clrscr();
int opt;

VARUN VARSHNEY
06996402712
do
{
cout<<"Menu"<<endl;
cout<<"1) View Records"<<endl;
cout<<"2) Insert Record"<<endl;
cout<<"3) Update Record"<<endl;
cout<<"4) Delete Record"<<endl;
cout<<"5) Selective View"<<endl;
cout<<"6) Exit"<<endl;
cout<<"Option ";
cin>>opt;
cout<<endl;
switch(opt)
{
case 1: viewRecords();
break;
case 2: insertRec();
break;
case 3: updateRec();
break;
case 4: char ch;
do{
deleteRec();
cout<<"Deletemore records?(Y/N): ";
cin>>ch;
}
while(toupper(ch)=='Y');
break;
case 5: selectiveView();
break;
case 6:cout<<"Exiting";exit(0);break;
default: cout<<"Incorrect Option!"<<endl;
}
cout<<endl;
}
while(opt!=0);
getch();
}

VARUN VARSHNEY
06996402712

VARUN VARSHNEY
06996402712

VARUN VARSHNEY
06996402712

You might also like