0
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.

Suwaid Aslam
  • 40
  • 1
  • 8
  • The solution is called *serialization*. C++ does not have built in support for serialization, so either you are going to have to invent your own, or you are going to have to use a serialization library like [this one](https://www.boost.org/doc/libs/1_73_0/libs/serialization/doc/index.html) – john Dec 26 '20 at 10:58
  • If you don't want to use a serialization library, then what you are going to have to do is save some information in the file that tells you what type is coming next. That way you will know what kind of type to read. – john Dec 26 '20 at 11:00
  • Incidentally it is not correct to use `read` and `write` on classes that have virtual functions. So the way you are trying at the moment (using read and write to read or write the whole object) cannot be made to work. – john Dec 26 '20 at 11:01
  • The code does not indicate any reason create any subclasses of `Room` here. In fact none of the subclasses provide any additional fields. No idea why you reimplement `getData` in every subclass instead of implementing it in `Room` once. All in all it seems like you'd be better off adding a `Room(const char* aType, int aPrice)` constructor and if necessary create factory methods for `Standard`, `Suite`, ect, if neccessary. In this case this would solve the problem completely, since you'd no longer need to distinguish types when reading. – fabian Dec 26 '20 at 11:33
  • Because I wanna do something like this : `cout<<"1. Standard"<>type; if(type==1) room = new Standard; else if(type==2) room = new Moderate; else if(type==3)` Let me know If I am doing something wrong cuz I am new to this concept. – Suwaid Aslam Dec 26 '20 at 12:41

1 Answers1

0

try this:

ofstream out;
out.open("rooms.txt");
out << obj1 << obj2 << ... << endl;

ifstream in;
in.open("rooms.txt");
in >> obj1 >> obj2 >> ...;

or you if you want to use still cin and cout:

freopen ("rooms.txt","w",stdin);
freopen ("rooms.txt","w",stdout);