You are on page 1of 10

/* PROGRAM CODE FOR INPUT, OUTPUT, SEARCH,

DELETE AND MODIFYING IN A BINARY FILE */

// Header files
#include<iostream.h>
#include<conio.h>
#include<fstream.h>
#include<stdio.h>
#include<string.h>
#include<process.h>

// Class containing data members and member functions…


class file
{
private:
int roll;
float age;
char name[100];
public:
void input();
void show();
char *getn()
{ return name;
}
}; file filobj;

void file::input()
{
cout<<" ENTER ROLL.NO OF THE STUDENT :- ";
cin>>roll;
cout<<" ENTER AGE OF THE STUDENT :- ";
cin>>age;
cout<<" ENTER NAME OF THE STUDENT :- ";
gets(name);
}

void file::show()
{
cout<<" ROLL.NO. OF THE STUDENT :- "<<roll<<endl;
cout<<" AGE OF THE STUDENT :- "<<age<<endl;
cout<<" NAME OF THE STUDENT :- "<<name<<endl;
}

// Functions
void create();
void add();
void display();
void displayp();
void modify();
void delet();

fstream fil;

void main()
{ int opt;
while(1)
{ clrscr();
cout<<"\n MAIN MENU "<<endl;
cout<<" 1. Create Data File "<<endl;
cout<<" 2. Add New Record In Data File "<<endl;
cout<<" 3. Display Record From Data File "<<endl;
cout<<" 4. Display Particular Record From Data File "<<endl;
cout<<" 5. Modify Particular Record From Data File "<<endl;
cout<<" 6. Delete Particular Record From Data File "<<endl;
cout<<" 7. Exit From Program "<<endl;
cin>>opt;
switch(opt)
{ case 1: { create(); // function for creating new data
cout<<" Display Main Menu"<<endl;
getch();
break;
}
case 2: { add(); // function for adding new data
cout<<" Display Main Menu"<<endl;
getch();
break;
}
case 3: { display(); // function for displaying data
cout<<" Display Main Menu"<<endl;
getch();
break;
}
case 4: { displayp(); // function for displaying particular record
cout<<" Display Main Menu"<<endl;
getch();
break;
}
case 5: { modify(); // function for modification in file
cout<<" Display Main Menu"<<endl;
getch();
break;
}
case 6: { delet(); // function for deleting particular record
cout<<" Display Main Menu"<<endl;
getch();
break;
}
case 7: { exit(0); // exit from program
}
default:{ cout<<" Wrong choice....Press Key For View The Main Menu";
getch();
clrscr();
}
}
}
getch();
}

void create() // creating new file


{ char ch='y';
fil.open("binary.dat", ios::noreplace | ios::binary);
while(ch=='y' || ch=='Y')
{ filobj.input();
fil.write((char *)&filobj,sizeof(filobj));
cout<<" Want To Continue......";
cin>>ch;
}
fil.close();
}
void add() // adding new record in file
{ char ch='y';
fil.open("binary.dat",ios::app |ios::binary);
while(ch=='y' || ch=='Y')
{ filobj.input();
fil.write((char *)&filobj,sizeof(filobj));
cout<<" Want To Continue...... ";
cin>>ch;
}
fil.close();
}

void display() // displaying record from file


{ fil.open("binary.dat",ios::in |ios::binary);
if(!fil)
{ cout<<" File Not Found!!!! ";
exit(0);
}
else
{ fil.read((char *)&filobj,sizeof(filobj));
while(!fil.eof())
{ filobj.show();
cout<<" Press Any Key For Next Record "<<endl;
getch();
fil.read((char *)&filobj,sizeof(filobj));
}
}
fil.close();
}

void displayp() // displaying particular record from file


{ char n[1000];
cout<<" Enter Name Of The Student That Should Be Searched : ";
gets(n);
fil.open("binary.dat",ios::in |ios::binary);
if(!fil)
{ cout<<" File Not Found ";
exit(0);
}
else
{ fil.read((char *)&filobj,sizeof(filobj));
while(!fil.eof())
{ if(strcmp(n,filobj.getn())==0)
{ filobj.show();
cout<<" Press Any Key "<<endl;
getch();
}
fil.read((char *)&filobj,sizeof(filobj));
}
}
fil.close();
}

void modify() // modifying desired record


{ char n[1000];
cout<<" Enter Name Of The Student That Should Be Searched : ";
gets(n);
fil.open("binary.dat",ios::in |ios::out |ios::binary);
if(!fil)
{ cout<<" File Not Found!!!! ";
exit(0);
}
else
{ fil.read((char *)&filobj,sizeof(filobj));
while(!fil.eof())
{ if(strcmp(n,filobj.getn())==0)
{ fil.seekg(0,ios::cur);
cout<<" Enter New Record... "<<endl;
filobj.input();
fil.seekp(fil.tellg()-sizeof(filobj));
fil.write((char *)&filobj,sizeof(filobj));
}
else
{ cout<<" Press Any Key "<<endl;
getch();
}
fil.read((char*)&filobj,sizeof(filobj));
}
}
fil.close();
}

void delet() // deleting desired record from file


{ char n[1000];
cout<<" Enter Name Of The Student That Should Be Deleted : ";
gets(n);
ofstream o;
o.open("new.dat",ios::noreplace |ios::out |ios::binary);
fil.open("binary.dat",ios::in |ios::binary);
if(!fil)
{ cout<<" File Not Found!!!! ";
exit(0);
}
else
{ fil.read((char *)&filobj,sizeof(filobj));
while(!fil.eof())
{ if(strcmp(n,filobj.getn())==0)
{ o.write((char *)&filobj,sizeof(filobj));
}
else
{ cout<<" Press Any Key "<<endl;
getch();
}
fil.read((char*)&filobj,sizeof(filobj));
}
}
o.close();
fil.close();
remove("binary.dat");
rename("new.dat","binary.dat");
}
// end of the program………
OUTPUTS :-

// for creating new file

// for adding new record in the file


// for displaying records from file

// for displaying particular record from file


// for modifying record from file

// for deleting particular record from file


// for coming out of program

You might also like