1

While making a program to practice file handling , I am facing an issue. During writing in a file I am seeing wierd stuff like below even I am saving my file in txt format and opening in notepad.

PIC of file

Can anyone clarify? Here is the code for writing:

int hostel::add()
{
int r,flag;
ofstream fout;
fout.open("model",ios::app);
cout<<"\n Enter Customer Details";
cout<<"\n **********************";
cout<<"\n\n Room no: ";
cin>>r;
flag=check(r);
if(flag){
cout<<"\n Sorry..!!!Room is already booked";
fout.close();
}
else

{
room_no=r;
cout<<"\n Name:\t ";
cin>>name;
cout<<"\n Address:\t ";
cin>>address;
cout<<"\n Phone No:\t ";
cin>>phone;
fout.write((char*)this,sizeof(*this));
fout.close();
cout<<"\n Your Room is booked!!";
    }

cout<<"\n Press any key to continue!!";
system("pause");
return 0;
}

Also when reading a file The last record gets printed 2 times:

reading of file pic

Code for reading is below:

int hostel::rooms()
{
    ifstream fin("model",ios::in);

    cout<<"\n\t\t\tList Of Rooms Allotted";
    cout<<"\n\n Room No.\tName\t\tAddress\t\tPhone No.\n";
    while(!fin.eof())
    {
        fin.read((char*)this,sizeof(*this));
        cout<<"\n\n"<<room_no<<"\t\t"<<name;
        cout<<"\t\t"<<address<<"\t\t"<<phone;
    }

    cout<<"\n\n\n\t\t\tPress any key to continue!!";
    system("pause");
    fin.close();
    return 0;
}

0 Answers0