u

Sunday 3 June 2012

C++ Program to modify a record after creation in a data file

C++ Program to modify a record after creation in a data file

#include<iostream.h>
#include<conio.h>
#include<fstream.h>
#include<string.h>
#include<stdio.h>
class stu
{
protected:
char na[20];
int p;
public :
void input(int n)
{
int i;
remove("s1.dat");
ofstream g2;
g2.open("s1.dat");

stu s;
for(i=1;i<=n;i++)
{
cout<<"enter name and percentage :";
cin>>s.na>>s.p;
if (i>n)
break;
else
{
g2.write((char*)&s,sizeof(s));
}
}
g2.close();
}
void modify()
{
char x[20],flag;
stu a;
ifstream g1;
g1.open("s1.dat");
ofstream g2;
g2.open("s2.dat");
cout<<"\n\n enter the name of the person ";
cin>>x;
while(!g1.eof())
{
g1.read((char*)&a,sizeof(a));
if(strcmp(x,a.na)==0)
{
flag='y';
cout<<"\n enter the new percentage ";
cin>>a.p;
}
if(!g1.eof())
{
g2.write((char*)&a,sizeof(a));
}
else
break;

}
if(flag!='y')
cout<<"\n sorry no such record present ";
g1.close();
g2.close();
remove("s1.dat");
rename("s2.dat" ,"s1.dat");
            }
void display()
{
stu a;
ifstream g3;
g3.open("s1.dat");
while(!g3.eof())
{
g3.read((char*)&a,sizeof(a));
if(!g3.eof())
{
cout<<"\n name is "<<a.na;
cout<<"\n percent is "<<a.p;
}
else
break;
}
g3.close();
}
};
void main()
{
int n;
clrscr();


stu s,a;
cout<<"enter the number of records ";
cin>>n;
s.input(n);
s.modify();
s.display();

getch();
} 

0 comments:

Post a Comment

Share

Twitter Delicious Facebook Digg Stumbleupon Favorites More