class Room{
public:
int price;
char type[20];
int roomNum;
int floor_num;
bool status; //reserved = true, not reserved = false
Room() : price(0) , status(false){
strcpy(type,"");
}
virtual ~Room(){
}
virtual void getData() = 0;
bool roomStatus(){ //return true if reserved and return false if not reserved
fstream readfile;
readfile.open("rooms.dat", ios::in | ios::out | ios::app);
bool flag = 0;
if(readfile.fail()){
cout<<"File Open Error!"<<endl;
readfile.open("rooms.dat", ios::in | ios::out | ios::trunc);
readfile.close();
}
else{
readfile.seekg(0,ios::end);
int n=readfile.tellg();
n=n/sizeof(this);
readfile.seekg(0,ios::beg);
for(int i=1;i<=n;i++){
Room *temp = this;
readfile.read(reinterpret_cast<char *>(temp), sizeof(Room));
if(temp->floor_num == this->floor_num && temp->roomNum == this->roomNum){
flag = 1;
break;
}
}
readfile.close();
return flag;
}
}
void save(){
ofstream outfile("rooms.dat", ios::app);
outfile.write(reinterpret_cast<char *>(this), sizeof(Room));
this->status = 1;
outfile.close();
cout<<"Room Reserved!"<<endl;
}
};
class Standard : public Room{
public:
Standard(){
price = 300;
strcpy(type,"Standard");
}
void getData(){
cout<<"Enter Floor Num:";
cin>>floor_num;
cout<<"Enter Room Num:";
cin>>roomNum;
}
};
class Moderate : public Room{
public:
Moderate(){
price= 500;
strcpy(type,"Moderate");
}
void getData(){
cout<<"Enter Floor Num:";
cin>>floor_num;
cout<<"Enter Room Num:";
cin>>roomNum;
}
};
class Superior : public Room{
public:
Superior(){
price= 1000;
strcpy(type,"Superior");
}
void getData(){
cout<<"Enter Floor Num:";
cin>>floor_num;
cout<<"Enter Room Num:";
cin>>roomNum;
}
};
class JuniorSuite : public Room{
public:
JuniorSuite(){
price = 2000;
strcpy(type,"Junior Suite");
}
void getData(){
cout<<"Enter Floor Num:";
cin>>floor_num;
cout<<"Enter Room Num:";
cin>>roomNum;
}
};
class Suite : public Room{
public:
Suite(){
price= 5000;
strcpy(type,"Suite");
}
void getData(){
cout<<"Enter Floor Num:";
cin>>floor_num;
cout<<"Enter Room Num:";
cin>>roomNum;
}
};
Here is my code. I want to save these objects in a single file called 'rooms.dat'. And I would like to read data from this exact same file in any of the objects. I am having trouble when I read data from that file it does not read that specific object's data. I know that this file has different types of data in it and the size of each object is also different, so when it reads data from that file it does not know what data to skip and what to read. Let me know if you have any solution to this problem that would be helpful.