Input Output Stream In CPP

#include<iostream>
#include<conio.h>
#include<fstream>
using namespace std;
class student
{
int roll;
char name[20];
public:
void addrecord()
{
fstream file;
file.open("student.dat",ios::app | ios::out | ios::binary);
if(!file)
{
cout<<"not found";
}
cout<<"enter rolll : ";
cin>>roll;
fflush(stdin);
cout<<"enter name :";
cin.getline(name,20);
file.write((char*)this,sizeof(student));
file.close();
}
void writerecord()
{
     fstream file;
file.open("student.dat",ios::app | ios::out | ios::binary);
if(!file)
{
cout<<"not found";
}
cout<<"enter rolll : ";
cin>>roll;
fflush(stdin);
cout<<"enter name :";
cin.getline(name,20);
file.write((char*)this,sizeof(student));
file.close();
}
     
void showrecord()
{
fstream file;
file.open("student.dat",ios::in | ios::binary);
if(!file)
{
cout<<"not found";
}
else
while(1)
{
file.read((char*)this,sizeof(student));
if(file.eof())

break;
cout<<this->roll<<this->name<<endl;
} 

file.close();
}
};
int main()
{
student s1;

s1.writerecord();
s1.showrecord();
getch();
}


Leave a Reply