mercredi 4 septembre 2019

unable to write to file in binary mode when the call to write is made through switch-case ladder

I'm trying create a .dat file where i'm trying to store object data by writing the data in binary mode. This works perfectly fine when i do this in the main() or in a loop, but things get weird when i try to do this task in a switch case. When i put the binary mode writing code in switch case, it doesn't write anything to the file!

class PlacementPortal
{
    private:
        int prn;
        char name[20];
        int marks;
    public:
        PlacementPortal():prn(0),name(""),marks(0){}
        void accept(){  .....   }       
        void display(){ ....    }
};
int main()
{
    ofstream fout("place.dat",ios::binary|ios::app);

    int c;
    PlacementPortal p;
    while(1)
    {
            cout<<"Enter 1 for add data, 2, for exit"<<endl;
        cin>>c;
        cin.get();
        switch(c)
        {
            case 1:     
                p.accept();
                    p.display();
                        fout.write((char*)&p,sizeof(PlacementPortal));
                    break;
            case 2:
                        exit(0);
                            break;
        }   }   
    return 0;
}

I expect the output to be written to the "place.dat" but only the file is created and it remains empty P.S. i've included fstream and iostream and also made sure that i'm not using datatype string.

Aucun commentaire:

Enregistrer un commentaire