#include<iostream.h>
#include<fstream.h>
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct stu
{
int roll;
char name[20];
char city[40];
}s;
void del()
{
int roll;
ofstream fout;
ifstream fin;
fin.open("skl.dat",ios::binary);
fout.open("temp.dat",ios::binary);
if(!fin)
{
cout<<"file not found\n";
getch();
exit(1);
}
cout<<"enter the roll u want to delete\n";
cin>>roll;
while(fin.read((char*)&s,sizeof(s)))
{
if(!(s.roll==roll))
{
// cout<<" not found\n";
// disp();
fout.write((char*)&s,sizeof(s));
}
else
{
cout<<" id found \n";
cout<<"name is \n";
puts(s.name);
}
}
int x;
fin.close();
fout.close();
x=rename("temp.dat","skl.dat");
remove("skl.dat");
if(x==0)
{
cout<<"unable\n";
}
}
void create()
{
char x;
ofstream fout;
fout.open("skl.dat",ios::binary);
if(!fout)
{
cout<<"file not found\n";
getch();
exit(1);
}
do
{
cout<<"enter the name \n";
gets(s.name);
cout<<"enter the roll \n";
cin>>s.roll;
cout<<"enter the city \n";
gets(s.city);
fout.write((char*)&s,sizeof(s));
cout<<"if u want to add more press y \n";
cin>>x;
}while(x=='y');
}
void display()
{
ifstream fin;
fin.open("skl.dat",ios::binary);
if(!fin)
{
cout<<"file not found";
getch();
exit(1);
}
while(fin.read((char*)&s,sizeof(s)))
{
cout<<"The name is ";
puts(s.name);
cout<<"\n";
cout<<"The roll is ";
cout<<s.roll<<"\n";
cout<<"The city is ";
puts(s.city);
cout<<"\n";
}
fin.close();
}
void search()
{
int roll1;
cout<<"enter the roll no which u want to search\n";
cin>>roll1;
ifstream fin;
fin.open("skl.dat",ios::binary);
fin.read((char*)&s,sizeof(s));
while(!fin.eof())
{
if(roll1==s.roll)
{
cout<<"FOUND\n";
cout<<"roll no"<<s.roll<<"\n";
cout<<"name ";
puts(s.name);
cout<<"city ";
puts(s.city);
break;
}
else
{
cout<<"not found\n";
break;
}
}
fin.close();
}
void main()
{
clrscr();
int ch;
char ch1='n';
do
{
cout<<"menu\n" ;
cout<<"1 creat\n";
cout<<"2 display\n";
cout<<"3 searching\n";
cout<<"4 delete\n";
cout<<"5 exit\n";
cout<<"enter your choice\n";
cin>>ch;
switch(ch)
{
case 1: create();
break;
case 2: display();
break;
case 3: search();
break;
case 4: del();
break;
case 5: exit(0);
break;
default:
cout<<"wrong choice\n";
}
}while(ch1=='n');
getch();
}
#include<fstream.h>
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct stu
{
int roll;
char name[20];
char city[40];
}s;
void del()
{
int roll;
ofstream fout;
ifstream fin;
fin.open("skl.dat",ios::binary);
fout.open("temp.dat",ios::binary);
if(!fin)
{
cout<<"file not found\n";
getch();
exit(1);
}
cout<<"enter the roll u want to delete\n";
cin>>roll;
while(fin.read((char*)&s,sizeof(s)))
{
if(!(s.roll==roll))
{
// cout<<" not found\n";
// disp();
fout.write((char*)&s,sizeof(s));
}
else
{
cout<<" id found \n";
cout<<"name is \n";
puts(s.name);
}
}
int x;
fin.close();
fout.close();
x=rename("temp.dat","skl.dat");
remove("skl.dat");
if(x==0)
{
cout<<"unable\n";
}
}
void create()
{
char x;
ofstream fout;
fout.open("skl.dat",ios::binary);
if(!fout)
{
cout<<"file not found\n";
getch();
exit(1);
}
do
{
cout<<"enter the name \n";
gets(s.name);
cout<<"enter the roll \n";
cin>>s.roll;
cout<<"enter the city \n";
gets(s.city);
fout.write((char*)&s,sizeof(s));
cout<<"if u want to add more press y \n";
cin>>x;
}while(x=='y');
}
void display()
{
ifstream fin;
fin.open("skl.dat",ios::binary);
if(!fin)
{
cout<<"file not found";
getch();
exit(1);
}
while(fin.read((char*)&s,sizeof(s)))
{
cout<<"The name is ";
puts(s.name);
cout<<"\n";
cout<<"The roll is ";
cout<<s.roll<<"\n";
cout<<"The city is ";
puts(s.city);
cout<<"\n";
}
fin.close();
}
void search()
{
int roll1;
cout<<"enter the roll no which u want to search\n";
cin>>roll1;
ifstream fin;
fin.open("skl.dat",ios::binary);
fin.read((char*)&s,sizeof(s));
while(!fin.eof())
{
if(roll1==s.roll)
{
cout<<"FOUND\n";
cout<<"roll no"<<s.roll<<"\n";
cout<<"name ";
puts(s.name);
cout<<"city ";
puts(s.city);
break;
}
else
{
cout<<"not found\n";
break;
}
}
fin.close();
}
void main()
{
clrscr();
int ch;
char ch1='n';
do
{
cout<<"menu\n" ;
cout<<"1 creat\n";
cout<<"2 display\n";
cout<<"3 searching\n";
cout<<"4 delete\n";
cout<<"5 exit\n";
cout<<"enter your choice\n";
cin>>ch;
switch(ch)
{
case 1: create();
break;
case 2: display();
break;
case 3: search();
break;
case 4: del();
break;
case 5: exit(0);
break;
default:
cout<<"wrong choice\n";
}
}while(ch1=='n');
getch();
}
No comments:
Post a Comment