mixing fstream streams issue even when closed streams
<p>I am having problems with the code bellow.  </p>

<p>It can write fine if i kill the read section.
It can read fine if i kill the write section and the file has already been written.
The 2 don't like each other.  It is like the write stream is not closed... though it should be.</p>

<p>What is wrong?</p>

<pre><code>#include "stdafx.h"
#include &lt;iostream&gt;
#include &lt;fstream&gt;
#include &lt;string&gt;
using namespace std;
using namespace System;

int main(array&lt;System::String ^&gt; ^args)
{
 string a[5];
 a[0]= "this is a sentence.";
 a[1]= "that";
 a[2]= "here";
 a[3]= "there";
 a[4]= "why";
 a[5]= "who";

 //Write some stuff to a file
 ofstream outFile("data.txt");     //create out stream
 if (outFile.is_open()){       //ensure stream exists
  for (int i=0; i&lt; 5; i++){
   if(! (outFile &lt;&lt; a[i] &lt;&lt; endl)){    //write row of stuff
    cout &lt;&lt; "Error durring writting line" &lt;&lt; endl; //ensure write was successfull
    exit(1);
   }
  }
  outFile.close();
 }

 //Read the stuff back from the file
 if(!outFile.is_open()){  //Only READ if WRITE STREAM was closed.
  string sTemp;   //temporary string buffer
  int j=0;    //count number of lines in txt file

  ifstream inFile("data.txt");  
  if (inFile.is_open()){
   while (!inFile.eof()){
    if(! (getline(inFile, sTemp)) ){    //read line into garbage variable, and ensure read of line was
     if(!inFile.eof()){       //successfull accounting for eof fail condition.
      cout &lt;&lt; "Error durring reading line" &lt;&lt; endl; 
      exit(1);
     }
    }
    cout &lt;&lt; sTemp &lt;&lt; endl;  //display line on monitor.
    j++;      
   }
   inFile.close();
  }
  cout &lt;&lt; (j -1) &lt;&lt; endl;  //Print number lines, minus eof line.
 }

 return 0;
}
</code></pre>
