Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- istream &operator>>(istream &in, InventoryItem &item) // Serialized binary extraction
- {
- char *descChar, *dateChar;
- int qty = 0;
- int descLength = 0;
- int dateLength = 0;
- double wholesale = 0.0;
- double retail = 0.0;
- // description
- in.read((char *)&descLength, sizeof(descLength)); // Read the length of the description
- descChar = new char[descLength];
- in.read(descChar, descLength);
- // dateAdded
- in.read((char *)&dateLength, sizeof(dateLength)); // Read the length of the date
- dateChar = new char[dateLength];
- in.read(dateChar, dateLength);
- // qtyOnHand
- in.read((char *)&qty, sizeof(qty));
- // wholesaleCost
- in.read((char *)&wholesale, sizeof(wholesale));
- // retailCost
- in.read((char *)&retail, sizeof(retail));
- // Store each char in a stringstream (to convert from Cstring to string)
- stringstream descSS;
- for(int i = 0; i < descLength; ++i)
- {
- descSS << descChar[i];
- }
- // Store each char in a stringstream (to convert from Cstring to string)
- stringstream dateSS;
- for(int i = 0; i < dateLength; ++i)
- {
- dateSS << dateChar[i];
- }
- item.description = descSS.str();
- item.dateAdded = dateSS.str();
- item.qtyOnHand = qty;
- item.wholesaleCost = wholesale;
- item.retailCost = retail;
- delete[] descChar;
- delete[] dateChar;
- descChar = NULL;
- dateChar = NULL;
- return in;
- }
- ostream &operator<<(ostream &out, const InventoryItem &item) // Serialized binary insertion
- {
- // description
- int descriptionLength = item.description.length();
- out.write((char *)&descriptionLength, sizeof(descriptionLength)); // Save the length of the description
- out.write(item.description.c_str(), descriptionLength);
- // dateAdded
- int dateLength = item.dateAdded.length();
- out.write((char *)&dateLength, sizeof(dateLength)); // Save the length of the date
- out.write(item.dateAdded.c_str(), dateLength);
- // qtyOnHand
- out.write((char *)&item.qtyOnHand, sizeof(item.qtyOnHand));
- // wholesaleCost
- out.write((char *)&item.wholesaleCost, sizeof(item.wholesaleCost));
- // retailCost
- out.write((char *)&item.retailCost, sizeof(item.retailCost));
- return out;
- }
Add Comment
Please, Sign In to add comment