kindly check the code for error as it is writing the record twice in the file. I think error maybe due to eof()
function. I have provided class description too just in case.
class student
{
int sno;
char sname[20];
float fees;
public:
void input()
{
cout << "enter sno,sname and fees \n";
cin >> sno;
gets(sname);
cin >> fees;
}
void output()
{
cout << sno << "\t" << sname << "\t" << fees << "\n";
}
int rsno()
{
return sno;
}
}s;
void add()
{
ofstream f1;
f1.open("stu.dat", ios::binary | ios::app);
s.input();
f1.write((char*)&s, sizeof(s));
f1.close();
}
void displayall()
{
ifstream f2;
f2.open("stu.dat", ios::binary);
while (!f2.eof())
{
f2.read((char*)&s, sizeof(s));
s.output();
}
f2.close();
}
void main()
{
add();
displayall()
}