title: Writing data with ofstream crash C++
body: <p>the IDE i'm using (visual studio 2012) doesn't show any errors or warnings, but i'm basicly trying to write binary data to a file called wizdata.bin and I have declared a few objects of my Wizard class. the Wizard class has few functions with 4 member variables ( 1 string and 3 ints). When I call the function in Wizard class called "save" the console program stops working, but it does manage to write the Wizard class member variable name which is a string. After that it fails to write any of the ints in the class. So the question is what am I doing wrong with trying to write ints to a binary file? the part that is commended in Wizard.cpp is causing the crash.</p>
  
  <p>heres my code files:</p>
  
  <p>Wizard.h:</p>
  
  <pre><code>    #ifndef WIZARD_H
      #define WIZARD_H
  
      #include &lt;string&gt;
      #include &lt;fstream&gt;
  
      class Wizard
      {
      public:
          Wizard();
          Wizard( std::string name, int hp, int mp, int armor );
  
          void print();
  
          void save( std::ofstream&amp; outFile );
          void load( std::ifstream&amp; inFile );
  
      private:
          std::string name;
          int hitPoints;
          int magicPoints;
          int armor;
      };
  
      #endif
  </code></pre>
  
  <p>Wizard.cpp:</p>
  
  <pre><code>    #include "Wizard.h"
      #include &lt;iostream&gt;
      using namespace std;
  
      Wizard::Wizard()
      {
          name = "Default";
          hitPoints = 0;
          magicPoints = 0;
          armor = 0;
      }
  
      Wizard::Wizard( string name, int hp, int mp, int armor )
      {
          this-&gt;name = name;
          hitPoints = hp;
          magicPoints = mp;
          this-&gt;armor = armor;
      }
  
      void Wizard::print()
      {
          cout &lt;&lt; "Name= " &lt;&lt; name &lt;&lt; endl;
          cout &lt;&lt; "HP= " &lt;&lt; hitPoints &lt;&lt; endl;
          cout &lt;&lt; "MP= " &lt;&lt; magicPoints &lt;&lt; endl;
          cout &lt;&lt; "Armor= " &lt;&lt; armor &lt;&lt; endl;
          cout &lt;&lt; endl;
      }
  
      void Wizard::save( ofstream&amp; outFile )
      {
          outFile.write( name.c_str(), name.size() );
          //outFile.write( (char*)hitPoints, sizeof(hitPoints) );
          //outFile.write( (char*)magicPoints, sizeof(magicPoints) );
          //outFile.write( (char*)armor, sizeof(armor) );
      }
  
      void Wizard::load( ifstream&amp; inFile )
      {
          inFile.read( (char*)name.c_str(), name.size() );
          inFile.read( (char*)&amp;hitPoints, sizeof(hitPoints) );
          inFile.read( (char*)&amp;magicPoints, sizeof(magicPoints) );
          inFile.read( (char*)&amp;armor, sizeof(armor) );
      }
  </code></pre>
  
  <p>and Main.cpp:</p>
  
  <pre><code>    #include "Wizard.h"
      using namespace std;
  
      int main()
      {
          Wizard wiz0( "Gandalf", 25, 100, 10 );
          Wizard wiz1( "Loki", 50, 150, 12 );
          Wizard wiz2( "Magius", 10, 75, 6 );
  
          ofstream outFile;
          outFile.open( "wizdata.bin", ios_base::binary );
  
          if( outFile )
          {
              wiz0.save( outFile );
              wiz1.save( outFile );
              wiz2.save( outFile );
  
              outFile.close();
          }
  
          return 0;
      }
  </code></pre>
  
  <p>this is the "Main.cpp" of my second console app that loads the data:</p>
  
  <pre><code>    #include "Wizard.h"
      #include &lt;iostream&gt;
      using namespace std;
  
      int main()
      {
          Wizard wiz0;
          Wizard wiz1;
          Wizard wiz2;
  
          cout &lt;&lt; "BEFORE LOADING..." &lt;&lt; endl;
          wiz0.print();
          wiz1.print();
          wiz2.print();
  
          ifstream inFile;
          inFile.open( "wizdata.bin", ios_base::binary );
  
          if( inFile )
          {
              wiz0.load( inFile );
              wiz1.load( inFile );
              wiz2.load( inFile );
  
              inFile.close();
          }
  
          cout &lt;&lt; "AFTER LOADING..." &lt;&lt; endl;
          wiz0.print();
          wiz1.print();
          wiz2.print();
  
          return 0;
      }
  </code></pre>
  
  <p>after loading the data this these are the values:
  Name: Gandalf
  HP: 25
  MP: 100
  Armor: 10</p>
  
  <p>Name: Loki2
  HP: 38400
  MP: 3072
  Armor: 1734429952</p>
  
  <p>Name: ius
  HP: 75
  MP: 6
  Armor: 0</p>