READING AND WRITING CLASS OBJECTS
< iostream.h >
< fstream.h >
< iomanip.h >
INVENTORY
{
name[15];
code;
cost;
:
readdata();
writedata();
};
INVENTORY::readdata()
{
std::cout << "Enter NAme:";
std::cin >> name;
std::cout << "Enter Code:";
std::cin >> code;
std::cout << "Enter cost:";
std::cin >> cost;
}
INVENTORY:: writedata()
{
std::cout<setiosflag(std::iostream::left) <<="" setw(10)="" name
<< setiosflags(std::iostream::right)
<< setw(10) << code
<< setprecision(2)
<< setw(10) << cost;
}
main()
{
INVENTORY item[3];
fstream file;
file.open("STOCK.DAT",std::iostream::in | std::iostream::out);
std::cout << "Enter Details for Three items \n";
(int i=0;i<3;i++)
{
item[i].readdata();
file.write((char *)&item[i],sizeof(item[i]));
}
file.seekg(0);
std::cout<<"\n OUTPUT\n\n";
(i=0;i<3;i++)
{
file.read((char *)&item[i],sizeof(item[i]));
item[i].writedata();
}
file.close();
}
|