FILES USING C++
/*PROGRAM USING FILES*/
PROGRAM:
#include<iostream.h>
#include<fstream.h>
#include<iomanip.h>
#include<string.h>
#include<conio.h>
class vote
{
protected:
char na[20];
int age;
char sex;
public:
void getdata();
void display();
};
void vote::getdata()
{
cout<<"\n Enter the name:";
cin>>na;
cout<<"\n Enter the age:";
cin>>age;
cout<<"\n Enter the sex:";
cin>>sex;
}
void vote::display()
{
cout<<"\n\t Name :"<<na;
cout<<"\n\t Age :"<<age;
if(age>18)
cout<<"\n\t"<<na<<"has rights to vote";
else
cout<<"\n\t"<<na<<"cannot vote";
cout<<"\n\t Sex :"<<sex;
}
void main()
{
clrscr();
vote vv;
fstream infile;
char fna[20];
int ch;
aa:
cout<<"\n\t Enter the file name(your choice):";
cin>>fna;
infile.open(fna,ios::in||ios::out);
vv.getdata();
infile.open(fna,ios::out);
infile.write((char *)&vv,sizeof(vv));
infile.close();
infile.open(fna,ios::in);
cout<<"\n\n Reading from file....";
infile.read((char *)&vv,sizeof(vv));
vv.display();
infile.close();
cout<<"\n\t Press i to continue.........";
cin>>ch;
if(ch==1)
goto aa;
else
getch();
}
OUTPUT
Enter the file name(your choice):A
Enter the name:vanitha
Enter the age:29
Enter the sex:Female
Reading from file....
Name :vanitha
Age :29
Punitha has rights to vote
Sex :F
Press i to continue.........
Comments
Post a Comment