title: How to make c++ function write on the last line of the output file each time it is called
body: <p>The question is how to make ouput file have <code>1</code> on the 1st line, <code>2</code> on the 2nd etc. because the program as it is rewrites the file everytime the loop is executed and you are left only with <code>9</code> in the output file.</p>
  
  <pre><code>   #include &lt;fstream&gt;
     using namespace std;
  
     void function (int i)
     { 
         ofstream output("result.out");
         output &lt;&lt; i &lt;&lt; endl;
         output.close();
     }
  
     int main()
     {
         for (int i=1; i&lt;10; i++)
         {
             function(i);
         }
         return 0;
     }
  </code></pre>