Binary file
#include <iostream>
#include <fstream>using namespace std;
struct Student
{
int roll;
string name;
};
int main()
{
fstream fout;
int i;
fout.open("student.dat", ios::out | ios::binary);
if(!fout)
{
cout<<"File could not be opened";
return 1;
}
Student st[3];
for(i = 0; i < 3; i++)
{
cout<<"Enter Student"<<i + 1<<" details(roll no, name): ";
cin>>st[i].roll;
cin>>st[i].name;
}
for(i = 0; i < 3; i++)
{
fout.write((char *)&st[i], sizeof(Student));
}
fout.close();
if(!fout.good())
{
cout<<"File corrupted while writing";
return 1;
}
}
Comments
Post a Comment