title: Is there a BinaryReader in C++ to read data written from a BinaryWriter in C#?
body: <p>I've written several ints, char[]s and the such to a data file with BinaryWriter in C#.  Reading the file back in (in C#) with BinaryReader, I can recreate all of the pieces of the file perfectly.  </p>
  
  <p>However, attempting to read them back in with C++ yields some scary results.  I was using fstream to attempt to read back the data and the data was not reading in correctly.  In C++, I set up an fstream with <code>ios::in|ios::binary|ios::ate</code> and used seekg to target my location.  I then read the next four bytes, which were written as the integer "16" (and reads correctly into C#).  This reads as 1244780 in C++ (not the memory address, I checked).  Why would this be?  Is there an equivalent to BinaryReader in C++?  I noticed it mentioned on msdn, but that's Visual C++ and intellisense doesn't even look like c++, to me.</p>
  
  <p>Example code for writing the file (C#):</p>
  
  <pre><code>    public static void OpenFile(string filename)
      {
          fs = new FileStream(filename, FileMode.Create);
          w = new BinaryWriter(fs);
  
      }
  
      public static void WriteHeader()
      {
          w.Write('A');
          w.Write('B');
      }
  
      public static byte[] RawSerialize(object structure)
      {
          Int32 size = Marshal.SizeOf(structure);
          IntPtr buffer = Marshal.AllocHGlobal(size);
          Marshal.StructureToPtr(structure, buffer, true);
          byte[] data = new byte[size];
          Marshal.Copy(buffer, data, 0, size);
          Marshal.FreeHGlobal(buffer);
          return data;
      }
  
      public static void WriteToFile(Structures.SomeData data)
      {
          byte[] buffer = Serializer.RawSerialize(data);
          w.Write(buffer);
      }
  </code></pre>
  
  <p>I'm not sure how I could show you the data file.</p>
  
  <p>Example of reading the data back (C#):</p>
  
  <pre><code>        BinaryReader reader = new BinaryReader(new FileStream("C://chris.dat", FileMode.Open));
          char[] a = new char[2];
          a = reader.ReadChars(2);
          Int32 numberoffiles;
          numberoffiles = reader.ReadInt32();
          Console.Write("Reading: ");
          Console.WriteLine(a);
          Console.Write("NumberOfFiles: ");
          Console.WriteLine(numberoffiles);
  </code></pre>
  
  <p>This I want to perform in c++.  Initial attempt (fails at first integer):</p>
  
  <pre><code> fstream fin("C://datafile.dat", ios::in|ios::binary|ios::ate);
   char *memblock = 0;
   int size;
   size = 0;
   if (fin.is_open())
   {
    size = static_cast&lt;int&gt;(fin.tellg());
    memblock = new char[static_cast&lt;int&gt;(size+1)];
    memset(memblock, 0, static_cast&lt;int&gt;(size + 1));
  
    fin.seekg(0, ios::beg);
    fin.read(memblock, size);
    fin.close();
    if(!strncmp("AB", memblock, 2)){ 
     printf("test. This works."); 
    }
    fin.seekg(2); //read the stream starting from after the second byte.
    int i;
    fin &gt;&gt; i;
  </code></pre>
  
  <p>Edit: It seems that no matter what location I use "seekg" to, I receive the exact same value.</p>