void employee::write()
{
etype type;
int size;
ofstream file("Data.txt",ios::binary);
for(int i=0;i<n;i++)
{
type=e[i]->gettype();
file.write(reinterpret_cast<char *>(&type),sizeof(type));
switch(type)
{
case managerr: size=sizeof(manager); break;
case laborerr: size=sizeof(laborer); break;
case scientistt: size=sizeof(scientist); break;
default: cout<<"Can't write type"<<endl;
}
file.write(reinterpret_cast<char *>(e[i]),size);
}
}
void employee::read()
{
etype type;
int size;
ifstream file("Data.txt",ios::binary);
while(true)
{
if(file.eof())
break;
file.read(reinterpret_cast<char *>(&type),sizeof(type));
switch(type)
{
case managerr: e[n]=new manager; size=sizeof(manager); break;
case laborerr: e[n]=new laborer; size=sizeof(laborer); break;
case scientistt: e[n]=new scientist; size=sizeof(scientist); break;
default: cout<<"Can't read type"<<endl;
}
file.read(reinterpret_cast<char *>(e[n]),size);
n++;
}
}
All the program works flawlessly except when I try to read the file and put it inside the variable again.
It crashes when I try to display the variables. I just inserted the code for writing and reading as this is the only problem in the code for the whole code it starts at page 608
New contributor
Mohamed Helmy is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.