# include <iostream.h>
# include <fstream.h>
# include <iomanip.h>
class LIST
{
char name[10];
int billno;
int amount_debited;
int received_amount;
int balance;
public:
void getdata (void)
{
cout << “Enter name: “; cin >> name;
cout << “Enter billno: “; cin >> billno;
cout << “Enter amount_debited: “; cin >> amount_debited;
cout << “Enter received_amount: “;
cin >> received_amount;
cout << “Enter balance: “; cin >> balance;
}
void putdata (void)
{
cout << setw (10) << name
<< setw (10) << billno
<< setw (10) << amount_debited
<< setw (10) << received_amount
<< setw (10) << balance
}
};
int main ()
{
List item;
fstream inoutfile;
inoutfile.open (“Person.DAT”, ios: : ate | ios: : in | ios: : out | ios: : binary);
inoutfile.seekg (0,ios : : beg);
cout << “PERSON LIST” << “\n”;
while(inoutfile.read ((char *) & item, sizeof item))
{
item.putdata ();
}
inoutfile.clear ();
/* Add one more Item */
cout<<”\n Add an Item\n”;
item.getdata ();
char ch;
cin.get(ch);
inoutfile.write((char *)&item,sizeof item);
inoutfile.seekg(0);
cout<<”CONTENTS OF APPEND FILE \n”;
while(inoutfile.read((char *) &item,sizeof item))
{
item.putdata();
}
/* Modify the details of a person */
cout <<”Enter object number to be updated \n”;
int object;
cin>> object;
cin.get (ch);
int location=(object-1)*sizeof(item);
if(inoutfile.eof())
inoutfile.clear();
inoutfile.seekp (location);
cout<<”Enter new values of the object”;
item.getdata();
cin.get(ch);
inoutfile.write((char *)*item,sizeof item)<<flush;
/* show updated file */
inoutfile.seekg (0);
cout << “Contents of updated file”;
while(inoutfile.read((char *)&item,sizeof item))
{
item.putdata();
}
inoutfile.close ();
return 0;
} |