Anh chị xem giùm em bài code này với ạ.
Vì sao mà nó không xuất ra màn hình được phần tử trong mảng dssv[100] chứa trong file nhị phân sau khi em gọi tới hàm doctep() ạ.
Em xin cảm ơn.
#include<iostream>
#include<fstream>
#include<string>
using namespace std;
class sinhvien
{
protected:
string ten;
int ma;
public:
void nhap()
{
fflush(stdin);
cout<<"\nTen: "; getline(cin,ten);
cout<<"\nMa: "; cin>>ma;
}
void xem()
{
cout<<"\nTen sv: "<<ten<<", Ma: "<<ma;
}
};
class danhsach: public sinhvien
{
private:
sinhvien dssv[100];
int tssv;
public:
void nhapbp()
{
cout<<"\nTong so sinh vien: "; cin>>tssv;
for(int i=0;i<tssv;i++)
{
dssv[i].nhap();
}
}
void xemmh()
{
cout<<"\ntssv = "<<tssv;
for(int i=0;i<tssv;i++)
{
dssv[i].xem(); }
}
void ghitep()
{
fstream fghi("file5.dat",ios::out|ios::binary);
fghi.write(reinterpret_cast<char*>(&tssv),sizeof(int));
for(int i=0;i<tssv;i++)
{
fghi.write(reinterpret_cast<char*>(&dssv[i]),sizeof(sinhvien));
}
fghi.close();
}
void doctep()
{
fstream fdoc("file5.dat",ios::in|ios::binary);
fdoc.read(reinterpret_cast<char*>(&tssv),sizeof(int));
for(int i=0;i<tssv;i++)
{
fdoc.read(reinterpret_cast<char*>(&dssv[i]),sizeof(sinhvien));
}
fdoc.close();
}
};
main()
{
danhsach x;
x.nhapbp(); x.ghitep();
x.doctep(); x.xemmh();
}