title: WINFORM C++ Managed string->unmanaged string produces unexpected results when combined with fstream
body: <p>Right. So I'm trying to do this:</p>
  
  <pre><code>    String^ ai = textBox4-&gt;Text;
  char* towrite = (char*) Marshal::StringToHGlobalAnsi(ai ).ToPointer();
  Marshal::FreeHGlobal(IntPtr(towrite));
  string write = string(towrite);
  
  ofstream filea;
  filea.open("Logtoreadfrom.txt");
  filea&lt;&lt; write; 
  filea.close(); 
  </code></pre>
  
  <p>Where I'm trying to copy the text from textBox4 to a file I create.</p>
  
  <p>I make the managed string "ai" the text from the text box,
  I convert it to the char towrite by marshalling from the StringToHGlobalAnsi command,
  I then define the unmanaged string write as the char towrite</p>
  
  <p>I define filea,
  I create filea,
  I write to file a,
  I close file a.</p>
  
  <p>When I open filea, My result typically consists of the random letters that have nothing to do with the text I input.</p>
  
  <p>I assume I'm doing something wrong, so could anyone point out where?</p>