title: C++ read/write to binary file. on program exit it gives System.AccessViolationException
body: <p>This C++ program is created using Visual Studio 2010. It's a group project that has everyone in class stumped.<br/>
  The program initially starts fine and the user can run through the program and add items that are written out to file. the items are read back in and displayed.<br/> When the user is done, on the program exiting <code>return 0;</code> it gives me "An unhandled exception of type System.AccessViolationException occurred. Attempted to read or write protected memory. This is often an indication that other memory is corrupt."<br/>
  When this happens it opens up a file called utility here => for (_Iterator_base12 **_Pnext = &amp;_Myproxy->_Myfirstiter; *_Pnext != 0; *_Pnext = (*_Pnext)->_Mynextiter) (*_Pnext)->_Myproxy = 0.<br/>
  I can fix this by replacing <code>return 0;</code> with <code>exit(0);</code><br/>
  I know it's not a real fix though and just a band-aid over a bullet hole that is causing this issue.<br/>
  After <em>fixing</em> (used very loosely here) that, then running the program again, it attempts to load the data file from the file system. It reads and loads the 1st item into a vector correctly but when it goes back to the start of the loop we see the same exception pop up, An unhandled exception of type System.AccessViolationException occurred.<br/>
  This is the first project we have worked on using fstream and binary i/o. We had worked through a similar program that was just reading and writing strings w/out any issues.<br/>I believe that the issue stems from something in the fileHandler class but am having a difficult time pinpointing what is causing this issue.<br/>Any advice/help is greatly appreciated.<br/><br/>Here is the code.<br/><br/><em>stdafx.h</em><br/></p>
  
  <pre><code>// stdafx.h : include file for standard system include files,
  // or project specific include files that are used frequently, but
  // are changed infrequently
  //
  #pragma once
  
  #include &lt;stdio.h&gt;
  #include &lt;iostream&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;sstream&gt;
  #include &lt;fstream&gt;
  #include &lt;string.h&gt;
  #include &lt;string&gt;
  #include &lt;vector&gt;
  #include &lt;time.h&gt;
  </code></pre>
  
  <p><br/><em>Week2.cpp</em>  (the main file for the project)<br/></p>
  
  <pre><code>//Week2.cpp *******************
  
  #include "stdafx.h"
  #include "fileHandler.h"
  
  
  using namespace std;
  using namespace System;
  
  
  int main(array&lt;System::String ^&gt; ^args)
  {   
  
  fileHandler theFile("store.pkl");
  vector&lt;item&gt; itemStack = theFile.getFile();
  
  cout &lt;&lt; "SKU  Name  Dept  Vendor    Max Order onHand" &lt;&lt; endl;
  cout &lt;&lt; "-------------------------------------------" &lt;&lt; endl;
  for (int i = 0; i &lt; itemStack.size(); i++)
  {
      cout &lt;&lt; itemStack[i].toString() &lt;&lt; endl;
  }
  vector&lt;item&gt; newStack;
  
  //prompt for input
  bool doneEditing = false;
  
  while(!doneEditing)
  {
      int A;
      int E;
      int F;
      int G;
      string B;
      string C;
      string D;
      string tempString;
      cout &lt;&lt; "Enter item info:" &lt;&lt; endl &lt;&lt; "Item SKU: ";
      cin &gt;&gt; A;
      cout &lt;&lt; endl &lt;&lt; "Item Name: ";
      cin &gt;&gt; B;
      cout &lt;&lt; endl &lt;&lt; "Item Dept: ";
      cin &gt;&gt; C;
      cout &lt;&lt; endl &lt;&lt; "Vendor Name: ";
      cin &gt;&gt; D;
      cout &lt;&lt; endl &lt;&lt; "Max Number: ";
      cin &gt;&gt; E;
      cout &lt;&lt; endl &lt;&lt; "Reorder Number: ";
      cin &gt;&gt; F;
      cout &lt;&lt; endl &lt;&lt; "OnHand Number: ";
      cin &gt;&gt; G;
      cout &lt;&lt; endl &lt;&lt; "Done?? Y/N: ";
      cin &gt;&gt; tempString;
      cout &lt;&lt; endl;
  
      item tempItem = item(A, B, C, D, E, F, G);
      newStack.push_back(tempItem);
  
      if (tempString == "Y" || tempString == "y")
      {
          doneEditing = true;
      }
      }
      cout &lt;&lt; "Saving stack to file" &lt;&lt; endl;
      theFile.putFile(newStack);
      cout &lt;&lt; "Items written to file" &lt;&lt; endl;
  
      vector&lt;item&gt; newFileStack = theFile.getFile();
  
      cout &lt;&lt; "After reload: " &lt;&lt; endl;
      cout &lt;&lt; "SKU  Name  Dept  Vendor    Max Order onHand" &lt;&lt; endl;
      cout &lt;&lt; "-------------------------------------------" &lt;&lt; endl;
      for (int i = 0; i &lt; newFileStack.size(); i++)
      {
      cout &lt;&lt; newFileStack[i].toString() &lt;&lt; endl;
      }
  
      cout &lt;&lt; "Thank you for using the Awesome Grocery Inventory Application" &lt;&lt; endl;
  
      system("PAUSE");
      /*return 0;   this breaks with same error as 
                    when reading in saved file after application restart 
                    */
      exit(0);
  }
  </code></pre>
  
  <p><br/><em>item.h</em><br/></p>
  
  <pre><code>using namespace std;
  
  #pragma once
  class item
  {
  public:
  item();
  item(int sku, string name, string dept, string vendor, int max, int reorder, int onhand);
  ~item(void);
  string toString();
  
  int ItemSKU() const;
  void ItemSKU(int val);
  string ItemName() const;
  void ItemName(string val);
  string VendorName() const;
  void VendorName(string val);
  int MaxNumb() const;
  void MaxNumb(int val);
  int ReorderNumb() const;
  void ReorderNumb(int val);
  int OnHandNumb() const;
  void OnHandNumb(int val);
  
  private:
  int itemSKU;
  string itemName;
  string itemDept;
  string vendorName;
  int maxNumb;
  int reorderNumb;
  int onHandNumb;
  };
  </code></pre>
  
  <p><br/><em>item.cpp</em><br/></p>
  
  <pre><code>#include "StdAfx.h"
  #include "item.h"
  
  using namespace std;
  
  item::item()
  {
  
  };
  item::item(int sku, string name, string dept, string vendor, int max, int reorder, int onhand)
  {
  itemSKU = sku;
  itemName = name;
  itemDept = dept;
  vendorName = vendor;
  maxNumb = max;
  reorderNumb = reorder;
  onHandNumb = onhand;
  }
  
  item::~item(void)
  {
  }
  
  string item::toString()
  {
  stringstream ss;
  ss &lt;&lt; itemSKU &lt;&lt; "\t" &lt;&lt; itemName &lt;&lt; "\t" &lt;&lt; itemDept &lt;&lt; "\t" &lt;&lt; vendorName &lt;&lt; "\t" &lt;&lt; maxNumb &lt;&lt; "\t" &lt;&lt; reorderNumb &lt;&lt; "\t" &lt;&lt; onHandNumb;
  string s = ss.str();
  return s;
  }
  
  int item::ItemSKU() const { return itemSKU; }
  void item::ItemSKU(int val) { itemSKU = val; }
  
  string item::ItemName() const { return itemName; }
  void item::ItemName(string val) { itemName = val; }
  
  string item::VendorName() const { return vendorName; }
  void item::VendorName(string val) { vendorName = val; }
  
  int item::MaxNumb() const { return maxNumb; }
  void item::MaxNumb(int val) { maxNumb = val; }
  
  int item::ReorderNumb() const { return reorderNumb; }
  void item::ReorderNumb(int val) { reorderNumb = val; }
  
  int item::OnHandNumb() const { return onHandNumb; }
  void item::OnHandNumb(int val) { onHandNumb = val; }
  </code></pre>
  
  <p><br/><em>fileHandler.h</em><br/></p>
  
  <pre><code>#include "item.h"
  
  using namespace std;
  
  #pragma once
  class fileHandler
  {
  public:
  fileHandler(string);
  ~fileHandler(void);
  
  vector&lt;item&gt; getFile();
  void putFile(vector&lt;item&gt; &amp;);
  
  private:
  string theFileName;
  };
  </code></pre>
  
  <p><br/><em>fileHandler.cpp</em><br/></p>
  
  <pre><code>#include "stdafx.h"
  #include "fileHandler.h"
  
  using namespace std;
  
  fileHandler::fileHandler(string name)
  {
  theFileName = name.c_str();
  }
  
  fileHandler::~fileHandler(void)
  {
  }
  
  
  vector&lt;item&gt; fileHandler::getFile()
  {
  ifstream inFile;
  string fileLine;
  vector&lt;item&gt; localStack;
  
  inFile.open(theFileName, ios::in|ios::binary);
  if (inFile)
  {
      cout &lt;&lt; "Getting file..." &lt;&lt; endl;
      cout &lt;&lt; endl;
      // not working on initial load if file is present at start
      inFile.seekg(0);
      while(!inFile.eof())
      {
          item tempItem;
          inFile.read(reinterpret_cast&lt; char * &gt;(&amp;tempItem), sizeof(item));
          localStack.push_back(tempItem);
          cout &lt;&lt; "item added to stack" &lt;&lt; endl;
      } //breaks from here after reading in 1 item from saved file on reopen
  } else {
      ofstream newFile;
      newFile.open(theFileName, ios::out|ios::binary);
      newFile.close();
      cout &lt;&lt; "Creating new file..." &lt;&lt; endl;
      cout &lt;&lt; endl;
      inFile.open(theFileName, ios::in|ios::binary);
  }
  inFile.clear();
  inFile.close();
  if (localStack.size() &gt; 0)
  {
      //removes some dirty data from end of stack
      localStack.pop_back();
  }
  return localStack;
  }
  
  void fileHandler::putFile( vector&lt;item&gt; &amp;items )
  {
  ofstream outFile;
  outFile.open(theFileName, ios::out|ios::binary);
  if(!outFile)
  {
      cerr&lt;&lt;"File could not be created"&lt;&lt;endl;
      system("pause");
      exit(1);
  }
  for (int i = 0; i &lt; items.size(); i++)
  {
  
      outFile.write(reinterpret_cast&lt;const char *&gt;(&amp;items[i]), sizeof(item));
  }
  outFile.clear();
  outFile.close();
  }
  </code></pre>