title: What am I doing wrong with my serializing a vector with structs in it to a .dat file?
body: <p>If I type in </p>
  
  <p>Description: Apple</p>
  
  <p>Quantity: 10</p>
  
  <p>Wholesale Cost: 30</p>
  
  <p>Retail Cost: 20</p>
  
  <p>Date Added: December</p>
  
  <p>These are the contents in my .dat file:</p>
  
  <p><strong>1Apple103020December</strong></p>
  
  <p>But when I load my program, it doesn't load the struct back in correctly resulting in there being 0 items in my list. Is that what it is suppose to look like or am I doing something seriously wrong.</p>
  
  <p>Code:</p>
  
  <pre><code>#include "stdafx.h"
  #include &lt;iostream&gt;
  #include &lt;fstream&gt;
  #include &lt;string&gt;
  #include &lt;vector&gt;
  
  using namespace System;
  using namespace std;
  #pragma hdrstop
  
  bool isValidChoice(int size, int choice);
  
  template&lt;typename T&gt;
  void writeVector(ofstream &amp;out, const vector&lt;T&gt; &amp;vec);
  
  template&lt;typename T&gt;
  vector&lt;T&gt; readVector(ifstream &amp;in);
  
  template&lt;typename T&gt;
  vector&lt;T&gt; addItem(vector&lt;T&gt; &amp;vec);
  
  template&lt;typename T&gt;
  void printItemDescriptions(vector&lt;T&gt; &amp;vec);
  
  template&lt;typename T&gt;
  int displayRecord(vector&lt;T&gt; &amp;vec);
  
  struct InventoryItem {
      string Description;
      int Quantity;
      int wholesaleCost;
      int retailCost;
      string dateAdded;
  } ;
  
  
  int main(void)
  {
      cout &lt;&lt; "Welcome to the Inventory Manager extreme! [Version 1.0]" &lt;&lt; endl;
      ifstream in("data.dat");
      if (in.is_open()) { cout &lt;&lt; "File \'data.dat\' has been opened successfully." &lt;&lt; endl; } else { cout &lt;&lt; "Error opening data.dat" &lt;&lt; endl;}
      cout &lt;&lt; "Loading data..." &lt;&lt; endl;
      vector&lt;InventoryItem&gt; structList = readVector&lt;InventoryItem&gt;( in );
      cout &lt;&lt;"Load complete." &lt;&lt; endl &lt;&lt; endl;
      in.close();
  
  
      while (1)
      {
  
          string line = "";
          cout &lt;&lt; "There are currently " &lt;&lt; structList.size() &lt;&lt; " items in memory.";
          cout &lt;&lt; endl &lt;&lt; endl;
          cout &lt;&lt; "Commands: " &lt;&lt; endl;
          cout &lt;&lt; "1: Add a new record " &lt;&lt; endl;
          cout &lt;&lt; "2: Display a record " &lt;&lt; endl;
          cout &lt;&lt; "3: Edit a current record " &lt;&lt; endl;
          cout &lt;&lt; "4: Delete a record " &lt;&lt; endl;
          cout &lt;&lt; "5: Save current information " &lt;&lt; endl;
          cout &lt;&lt; "6: Exit the program " &lt;&lt; endl;
          cout &lt;&lt; endl;
          cout &lt;&lt; "Enter a command 1-6: ";
  
          getline(cin , line);
  
          int rValue = atoi(line.c_str());
  
          system("cls");
  
          ofstream out("data.dat");
  
          switch (rValue)
          {
              case 1:
                  addItem(structList);
                  break;
              case 2:
                  displayRecord(structList);
                  break;
              case 3:
                  break;
              case 4:
                  break;
              case 5:
                  if (!structList.size()) { cout &lt;&lt; "There are no items to save! Enter one first!" &lt;&lt; endl &lt;&lt; endl; system("pause"); system("cls"); break; }
                  writeVector(out , structList);
                  break;
              case 6:
                  return 0;
              default:
                  cout &lt;&lt; "Command invalid. You can only enter a command number 1 - 6. Try again. " &lt;&lt; endl;
          }
  
          out.close();
      }
  
      system("pause");
  
      return 0;
  }
  
  template&lt;typename T&gt;
  void writeVector(ofstream &amp;out, const vector&lt;T&gt; &amp;vec)
  {
      out &lt;&lt; vec.size();
  
      for(vector&lt;T&gt;::const_iterator i = vec.begin(); i != vec.end(); i++)
      {
          out &lt;&lt; *i;
      }
      cout &lt;&lt; "Save completed!" &lt;&lt; endl &lt;&lt; endl;
  }
  
  ostream &amp;operator&lt;&lt;(ostream &amp;out, const InventoryItem &amp;i)
  {
      out &lt;&lt; i.Description;
      out &lt;&lt; i.Quantity;
      out &lt;&lt; i.wholesaleCost &lt;&lt; i.retailCost;
      out &lt;&lt; i.dateAdded;
      return out;
  }
  
  istream &amp;operator&gt;&gt;(istream &amp;in, InventoryItem &amp;i)
  {
      in &gt;&gt; i.Description;
      in &gt;&gt; i.Quantity;
      in &gt;&gt; i.wholesaleCost &gt;&gt; i.retailCost;
      in &gt;&gt; i.dateAdded;
      return in;
  }
  
  
  
  template&lt;typename T&gt;
  vector&lt;T&gt; readVector(ifstream &amp;in)
  {
      size_t size;
      if (in.fail())
      {
      in &gt;&gt; size;
      } else {
          size = 0;
      }
  
      vector&lt;T&gt; vec;
      vec.reserve(size);
  
      for(unsigned int i = 0; i &lt; size; i++)
      {
          T tmp;
          in &gt;&gt; tmp;
          vec.push_back(tmp);
      }
  
      return vec;
  }
  
  template&lt;typename T&gt;
  vector&lt;T&gt; addItem(vector&lt;T&gt; &amp;vec)
  {
      system("cls");
  
      string word;
      unsigned int number;
  
      InventoryItem newItem;
  
      cout &lt;&lt; "-Add a new item-" &lt;&lt; endl &lt;&lt; endl;
      cout &lt;&lt; "Enter the description for the item: ";
      getline (cin , word);
      newItem.Description = word;
  
      cout &lt;&lt; endl;
      cout &lt;&lt; "Enter the quantity on hand for the item: ";
      getline (cin , word);
      number = atoi(word.c_str());
      newItem.Quantity = number;
  
      cout &lt;&lt; endl;
      cout &lt;&lt; "Enter the Retail Cost for the item: ";
      getline (cin , word);
      number = atoi(word.c_str());
      newItem.retailCost = number;
  
      cout &lt;&lt; endl;
      cout &lt;&lt; "Enter the Wholesale Cost for the item: ";
      getline (cin , word);
      number = atoi(word.c_str());
      newItem.wholesaleCost = number;
  
      cout &lt;&lt; endl;
      cout &lt;&lt; "Enter current date: ";
      getline (cin , word);
      newItem.dateAdded = word;
  
      vec.push_back(newItem);
  
      return vec;
  }
  
  template&lt;typename T&gt;
  void printItemDescriptions(vector&lt;T&gt; &amp;vec)
  {
      int size = vec.size();
  
      if (size)
      {
          cout &lt;&lt; "---------------------------------" &lt;&lt; endl;
          cout &lt;&lt; "|      ~ Item Descriptions ~    |" &lt;&lt; endl;
          cout &lt;&lt; "---------------------------------" &lt;&lt; endl;
          cout &lt;&lt; "*********************************" &lt;&lt; endl;
          for (int i = 0 ; i &lt; size ; i++)
          {
              cout &lt;&lt; "(" &lt;&lt; i+1 &lt;&lt; ")" &lt;&lt; ": " &lt;&lt; vec[i].Description &lt;&lt; endl;
          }
          cout &lt;&lt; "*********************************" &lt;&lt; endl &lt;&lt; endl;
      }
  }
  
  template&lt;typename T&gt;
  int displayRecord(vector&lt;T&gt; &amp;vec)
  {
      string word = "";
      string quit = "quit";
      int choice = 1;
      int size = vec.size();
  
      if (size)
      {
          printItemDescriptions(vec);
          cout &lt;&lt; endl;
  
          while (1)
          {
              cout &lt;&lt; "Type \"exit\" to return to the Main Menu." &lt;&lt; endl &lt;&lt; endl;
              cout &lt;&lt; "Enter \"list\" to re-display the items." &lt;&lt; endl &lt;&lt; endl;
              cout &lt;&lt; endl;
              cout &lt;&lt; "Pick the number of the item you would like to display: ";
              getline (cin , word);
  
              if (convertToLower(word) == "exit") { system("cls"); return 0; }
              if (convertToLower(word) == "list") { system("cls"); displayRecord(vec); }
  
              choice = atoi(word.c_str());
  
              if (isValidChoice(size, choice))
              {
                  system("cls");
                  cout &lt;&lt; endl &lt;&lt; "[Item (" &lt;&lt; choice &lt;&lt; ") details] " &lt;&lt; endl &lt;&lt; endl;
                  cout &lt;&lt; "******************" &lt;&lt; endl;
                  cout &lt;&lt; "*  Description   * " &lt;&lt; vec[choice-1].Description &lt;&lt; endl;
                  cout &lt;&lt; "******************" &lt;&lt; endl &lt;&lt; endl;
                  cout &lt;&lt; "******************" &lt;&lt; endl;
                  cout &lt;&lt; "*Quantity On Hand* " &lt;&lt; vec[choice-1].Quantity &lt;&lt; endl;
                  cout &lt;&lt; "******************" &lt;&lt; endl &lt;&lt; endl;
                  cout &lt;&lt; "******************" &lt;&lt; endl;
                  cout &lt;&lt; "* Wholesale Cost * " &lt;&lt; vec[choice-1].wholesaleCost &lt;&lt; endl;
                  cout &lt;&lt; "****************** " &lt;&lt; endl &lt;&lt; endl;
                  cout &lt;&lt; "******************" &lt;&lt; endl;
                  cout &lt;&lt; "*  Retail Cost   * " &lt;&lt; vec[choice-1].retailCost &lt;&lt; endl;
                  cout &lt;&lt; "****************** " &lt;&lt; endl &lt;&lt; endl;
                  cout &lt;&lt; "******************" &lt;&lt; endl;
                  cout &lt;&lt; "*  Data Added    * " &lt;&lt; vec[choice-1].dateAdded &lt;&lt; endl;
                  cout &lt;&lt; "****************** " &lt;&lt; endl &lt;&lt; endl;
              } else { system("cls"); cout &lt;&lt; "That item doesn't exist!" &lt;&lt; endl; cout &lt;&lt; "Pick another item or enter \"list\" to see available items." &lt;&lt; endl &lt;&lt; endl; }
          }
      } else { cout &lt;&lt; "There are currently no items to display." &lt;&lt; endl &lt;&lt; endl; system("pause"); system("cls"); return 0; }
  
      return 1;
  }
  
  bool isValidChoice(int size, int choice)
  {
      for (int i = 0 ; i &lt;= size ; i++)
      {
          if (choice == size) { return true; }
      }
      return false;
  }
  
  string convertToLower(string word)
  {
      for (unsigned int i = 0 ; i &lt; word.size() ; i++)
      {
          word[i] = tolower(word[i]);
      }
  
      return word;
  }
  </code></pre>