1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
|
class store
{
int ino, stock;
char iname[100];
float price;
char chline;
public:
void NewItem()
{
cout<<"\n\nNew Item Registration:\n\nItem No.: "; cin>>ino;
cout<<"\nItem Name: "; cin>>ws; std::cin.getline(iname,100,'\n');
cout<<"\nPrice: "; cin>>price;
cout<<"\nQuantity: "; cin>>stock;
cout<<"\nRegistered!";
}
int returnIno()
{
return ino;
}
void Details()
{
cout<<"\n\nItem No.: "<<ino<<"\nItem Name: "<<iname<<"\nPrice: "<<price<<"\nStock: "<<stock;
}
void SaveToStream (ofstream& os)
{
os.write ((const char *)&ino, sizeof (ino));
os.write ((const char *)&stock, sizeof (stock));
os.write ((const char *)iname, sizeof (iname));
os.write ((const char *)&chline, sizeof (chline));
os.write ((const char *)&price, sizeof (price));
}
ifstream& LoadFromStream (ifstream& is)
{
is.read ((char *)&ino, sizeof (ino));
is.read ((char *)&stock, sizeof (stock));
is.read ((char *)iname, sizeof (iname));
is.read ((char *)&chline, sizeof (chline));
is.read ((char *)&price, sizeof (price));
return is;
}
};
void OpenFile()
{
system("CLS");
char str[100];
store s;
cout<<"\n\nEnter File Name to display: "; cin>>ws; std::cin.getline(str,100,'\n');
ifstream f1;
f1.open(str, ios::binary);
cout<<"\n\nShowing Contents of the File";
while(s.LoadFromStream(f1))
{
s.Details();
}
f1.close();
}
void InsertEntry()
{
system("CLS");
store snew,sold;
ifstream f1;
f1.open("stores.dat", ios::binary);
ofstream f2;
f2.open("temp.dat", ios::binary);
cout<<"\nYou're about to Insert a New Item in the existing File";
snew.NewItem();
char last='y';
while(sold.LoadFromStream(f1))
{
if(snew.returnIno()<=sold.returnIno())
{
snew.SaveToStream(f2);
last='n';
break;
}
else
{
sold.SaveToStream(f2);
}
}
if(last=='y')
{
snew.SaveToStream(f2);
}
else
{
while(sold.LoadFromStream(f1))
{
sold.SaveToStream(f2);
}
}
f1.close();
f2.close();
remove("stores.dat");
rename("temp.dat","stores.dat");
OpenFile();
}
|