title: Writing binary file in C# and then reading the same file in C++
body: <p>I am writing a binary file in C# using the BinaryWriter class</p>
  
  <pre><code>using (var b = new System.IO.BinaryWriter(System.IO.File.Open("C:\\TextureAtlas0.txa", 
         System.IO.FileMode.Create)))
  {
    int count;
  
    // Write the number of source rectangle entries
    count = textureAtlas.Rectangles.Count;
  
    b.Write(count);
  
    for (int i = 0; i &lt; count; ++i)
    {
      b.Write(textureAtlas.SpriteNames[i]);
      b.Write(textureAtlas.Rectangles[i].X);
      b.Write(textureAtlas.Rectangles[i].Y);
      b.Write(textureAtlas.Rectangles[i].Width);
      b.Write(textureAtlas.Rectangles[i].Height);
    }
  }
  </code></pre>
  
  <p>I then try and read the same file into C++ using the following steps.</p>
  
  <p>I have a struct that holds the data in the same order it was written.</p>
  
  <pre><code>struct TextureAtlasEntry 
  {
      std::string name;
      int x;
      int y;
      int width;
      int height;
  };
  </code></pre>
  
  <p>The count is read first</p>
  
  <pre><code>int count;
  
  fread(&amp;count, sizeof(int), 1, LoadFile);
  </code></pre>
  
  <p>I then attempt to use the count value to determine the list size that will hold the data.  I can't seem to use an array as the value for count will be different depending on the file that is read.</p>
  
  <pre><code>std::list&lt;TextureAtlasEntry&gt; entries;
  fread(&amp;entries, sizeof(TextureAtlasEntry), count, LoadFile);
  
  fclose(LoadFile);
  </code></pre>
  
  <p>The code above does not work.  I can get the count to be read correctly but the memcpy command causes an access violation with fread and the entries list.</p>
  
  <p>How can I read the data correctly and should I swap fread with a C++ equivalent?</p>
  
  <p>EDIT:</p>
  
  <p>I can now read the entire binary file into memory using the following code</p>
  
  <pre><code>ifstream::pos_type size;
  char *memblock;
  
  ifstream file ("TextureAtlas0.txa", ios::in|ios::binary|ios::ate);
  
  if (file.is_open())
  {
      size = file.tellg();
      memblock = new char[size];
      file.seekg (0, ios::beg);
      file.read (memblock, size);
      file.close();
  
      printf("File content is in memory");
  
      delete[] memblock;
  }
  else 
  {
      printf("Unable to open file");
  }
  </code></pre>
  
  <p>Now the file is in memory, how do I convert the char[] data into struct data?</p>