title: Corrupted file stream (writing in c# and reading in c++)
body: <p>I would really appreciate some help with a problem I'm having.</p>
  
  <p>I need to save binary files in C# and read them in C++ (editor / game-engine). My current solution has been working for years but now, for some reason, I'm starting to have problems.</p>
  
  <p>In C#, I have the following code to setup a stream:</p>
  
  <pre><code>Stream stream = new FileStream(fileName, FileMode.Create, FileAccess.Write, FileShare.None);
  BinaryWriter writer = new BinaryWriter(stream);
  </code></pre>
  
  <p>And I write data using writer.Write(..).</p>
  
  <p>In C++, I open a stream using this code:</p>
  
  <pre><code>std::ifstream inStream;
  inStream.open(fileName, std::ios::binary);
  </code></pre>
  
  <p>And I read data like this:</p>
  
  <pre><code>int val = -1;
  inStream.read((char*) &amp;val, sizeof(int)).
  </code></pre>
  
  <p>99% of the time everything works fine but sometimes when I read the file (in C++) the stream gets screwed up and the fail-bit gets set. </p>
  
  <p>The problem always occurs at the same place in an exported file and when this happens it will happen every time I try to read the file.</p>
  
  <p>It can be when I'm reading an int, a float, a string or whatever. It can also happen anywhere in the stream.</p>
  
  <p>I also build an import feature in the editor and I never run into this problem in C#.</p>
  
  <p>So, apparently I'm missing something when I read the file in C++.</p>
  
  <p>I don't have any real experience with streams etc so I guess (and hope) that I'm just missing something when I setup the streams.</p>
  
  <p>Thanks for your help!</p>