title: Reading / Writing Shape (graphics) Data to and From a Binary File
body: <p>(I apologize if this is a dumb question; I'm really new to this and don't  have much experience with binary files yet)</p>
  
  <p>I'm trying to make a sort of poor-man's MS paint program where you can draw shapes with your mouse, change colors, brush, ect. I need to be able to save the user's progress into a binary file (homework requirements) by means of a struct.</p>
  
  <p>I think I've managed to save the data successfully(?), but I can't seem to extract it back out again -- or at least, I'm having trouble figuring out how to check whether or not there's already anything in the file. No matter how I write it, the compiler doesn't seem interested in reading it in.</p>
  
  <p><strike>
  Honestly, I might have just gotten myself twisted up with the pointers</strike></p>
  
  <p>If I get rid of the 'while' in front of "<strong>inFile.read(reinterpret_cast(&amp;temp), sizeof(ShapeData))</strong>" in order to force it to read from the file, it just (not all too surprisingly) returns garbage.</p>
  
  <p>I've written out a 'bug-test' version of it (getting rid of unrelated functions) to try and isolate the problem as much as I can, but I've been at it for hours and could really use the advice of someone who actually fully knows what they're doing. :)</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;fstream&gt;
  using namespace std;
  #include &lt;SFML\Graphics.hpp&gt;
  using namespace sf;
  
  enum ShapeEnum { CIRCLE, SQUARE };
  
  struct ShapeData
  {
      ShapeEnum shapeType;
      int shapeLocationX,
      shapeLocationY,     // location x and y originally vector2f;
      shapeColor;         // had more luck with changing to ints for file
                          // (shape color also converted from .tointeger())
  };
  
  int main()
  {
  
    int amountFilled = 0;
    ShapeData temp;
  
    vector&lt;ShapeData*&gt; shapes;
  
    ofstream oFile;
    oFile.open("shapes.bin", ios::out | ios::binary);
    ifstream inFile;
    inFile.open("shapes.bin", ios::in | ios::binary);
  
    if (!inFile)
      {
         cout &lt;&lt; "file Open Failure" &lt;&lt; endl;
         EXIT_FAILURE;
      }
  
    while (inFile.read(reinterpret_cast&lt;char*&gt;(&amp;temp), sizeof(ShapeData)))
    {
       shapes.push_back(new ShapeData(temp));
       amountFilled++;
    }
  
       // test input
          temp.shapeColor = Color::Blue.toInteger();
          temp.shapeLocationX = 200;
          temp.shapeLocationY = 400;
          temp.shapeType = CIRCLE;
          shapes.push_back(new ShapeData(temp));
          amountFilled++;
       // test input
          temp.shapeColor = Color::Red.toInteger();
          temp.shapeLocationX = 25;
          temp.shapeLocationY = 650;
          temp.shapeType = SQUARE;
          shapes.push_back(new ShapeData(temp));
          amountFilled++; 
  
    for (int i = 0; i &lt; amountFilled; i++) // test display
    {
       cout &lt;&lt; "Color # : " &lt;&lt; shapes[i]-&gt;shapeColor &lt;&lt; "; " 
       &lt;&lt; "Location : ( " 
           &lt;&lt; shapes[i]-&gt;shapeLocationX &lt;&lt; ", " &lt;&lt; shapes[i]-&gt;shapeLocationY 
           &lt;&lt; " ); " 
       &lt;&lt; "Enum # : " &lt;&lt; shapes[i]-&gt;shapeType &lt;&lt; endl;
  
       oFile.write(reinterpret_cast&lt;char*&gt;(&amp;shapes[i]), sizeof(ShapeData));
    }
  
          oFile.close();
          inFile.close();
  
      return 0;
  }
  </code></pre>
  
  <p>the way that it's currently written, the output looks like this :</p>
  
  <p><strong>Color # : 65535; Location : ( 200, 400 ); Enum # : 0</strong></p>
  
  <p><strong>Color # : -16776961; Location : ( 25, 650 ); Enum # : 1</strong></p>
  
  <p>When I remove the lines that add in new shapes to the vector, it doesn't put anything into the vector or display at all; I need to figure out how to add what was already in the file <em>in addition to</em> new shapes.</p>