Guest User

Untitled

a guest
Jul 20th, 2018
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.11 KB | None | 0 0
  1. istream &operator>>(istream &in, InventoryItem &item) // Serialized binary extraction
  2. {
  3.     char *descChar, *dateChar;
  4.     int qty = 0;
  5.     int descLength = 0;
  6.     int dateLength = 0;
  7.     double wholesale = 0.0;
  8.     double retail = 0.0;
  9.  
  10.     // description
  11.     in.read((char *)&descLength, sizeof(descLength)); // Read the length of the description
  12.     descChar = new char[descLength];
  13.     in.read(descChar, descLength);
  14.  
  15.     // dateAdded
  16.     in.read((char *)&dateLength, sizeof(dateLength)); // Read the length of the date
  17.     dateChar = new char[dateLength];
  18.     in.read(dateChar, dateLength);
  19.  
  20.     // qtyOnHand
  21.     in.read((char *)&qty, sizeof(qty));
  22.  
  23.     // wholesaleCost
  24.     in.read((char *)&wholesale, sizeof(wholesale));
  25.  
  26.     // retailCost
  27.     in.read((char *)&retail, sizeof(retail));
  28.  
  29.     // Store each char in a stringstream (to convert from Cstring to string)
  30.     stringstream descSS;
  31.     for(int i = 0; i < descLength; ++i)
  32.     {
  33.         descSS << descChar[i];
  34.     }
  35.  
  36.     // Store each char in a stringstream (to convert from Cstring to string)
  37.     stringstream dateSS;
  38.     for(int i = 0; i < dateLength; ++i)
  39.     {
  40.         dateSS << dateChar[i];
  41.     }
  42.  
  43.     item.description = descSS.str();
  44.     item.dateAdded = dateSS.str();
  45.     item.qtyOnHand = qty;
  46.     item.wholesaleCost = wholesale;
  47.     item.retailCost = retail;
  48.  
  49.     delete[] descChar;
  50.     delete[] dateChar;
  51.     descChar = NULL;
  52.     dateChar = NULL;
  53.  
  54.     return in;
  55. }
  56.  
  57. ostream &operator<<(ostream &out, const InventoryItem &item) // Serialized binary insertion
  58. {
  59.     // description
  60.     int descriptionLength = item.description.length();
  61.     out.write((char *)&descriptionLength, sizeof(descriptionLength)); // Save the length of the description
  62.     out.write(item.description.c_str(), descriptionLength);
  63.  
  64.     // dateAdded
  65.     int dateLength = item.dateAdded.length();
  66.     out.write((char *)&dateLength, sizeof(dateLength)); // Save the length of the date
  67.     out.write(item.dateAdded.c_str(), dateLength);
  68.  
  69.     // qtyOnHand
  70.     out.write((char *)&item.qtyOnHand, sizeof(item.qtyOnHand));
  71.  
  72.     // wholesaleCost
  73.     out.write((char *)&item.wholesaleCost, sizeof(item.wholesaleCost));
  74.  
  75.     // retailCost
  76.     out.write((char *)&item.retailCost, sizeof(item.retailCost));
  77.  
  78.     return out;
  79. }
Add Comment
Please, Sign In to add comment