C++ File Handling | Switch Read - Write Record
Q. WAP to Write and Read a record from a file using switch case. Download
#include<iostream.h>
#include<conio.h>
#include<fstream.h>
class student
{
int rollno;
char name[20];
public:
void input();
void output();
};
void student::input()
{
cout<<"Enter rollno: ";
cin>>rollno;
cout<<"Enter Name: ";
cin>>name;
}
void student::output()
{
cout<<"\nRollno: "<<rollno;
cout<<"\nName: "<<name;
}
void main()
{
int ch;
student s, s2;
fstream file;
file.open("record.dat",ios::in|ios::out|ios::app);
clrscr();
cout<>ch;
switch(ch)
{
case 1: //read
file.seekg(0);
do{
file.read((char *) &s2, sizeof(s2));
s2.output();
}while(file);
break;
case 2: //write
s.input();
file.write((char *) &s, sizeof(s));
break;
}
file.close();
getch();
}