title: writing into file (std::ofstream) in a loop writes last line only
body: <p>I'm using this code to extract certain part of each line of a text file:</p>
  
  <pre><code>std::ifstream file( "infile.txt" );
  std::string in1, out1;
  int blockNumber = 0;
  
  while( getline( file, in1 ) ) 
  {   
      int n = 0;
      int i = 0;
  
      while( i &lt;= blockNumber )
      {   
          n = in1.find_first_of("(", n + 1); 
          i++;
      }   
      out1 = in1.substr( n + 1, ( in1.find_first_of(")", n) - n - 1) );  
      ofstream fmatch ("solo_matches.txt",ios::out);
      fmatch.close();
      fmatch.open("solo_matches.txt");
      fmatch &lt;&lt; out1;
      fmatch.close();
  } 
  </code></pre>
  
  <p>But when I run the code, the result is not as I expect it to be. Only the last string is being written to the file. If I use this instead: </p>
  
  <pre><code> std::cout &lt;&lt; out1 &lt;&lt; std::endl;
  </code></pre>
  
  <p>I get the exact output I need. I do not understand what is the difference.</p>