title: How to write multiple lines to file in C++ without losing previous lines
body: <p>I need to write multiple lines in a txt and binary file and I created a function (I'll attach it below) that's supposed to write a few array items into the file. The problem is, I used fprintf while I open and close the file in the same function and so every time my for (because it's an array) calls the function, it overwrites the file. I'm thinking of opening the file and closing it before and after the for, respectively. Is there a more elegant way of doing what I'm after? Currently I'm using <a href="https://www.tutorialspoint.com/cprogramming/c_file_io.htm" rel="nofollow noreferrer">File I/O</a> for consulting purposes and it only mentions fprintf.</p>
  <p>Here's my code:</p>
  <pre><code>#include &lt;iostream&gt;
  #include &lt;stdio.h&gt;
  #include &lt;fstream&gt;
  
  using namespace std;
  
  struct numarComplex {
      int parteReala = 0;
      int parteFractionara = 0;
  }numere[5];
  
  void printareNumar (struct numarComplex numar) {
      printf(&quot;Numarul este: %d.%d\n&quot;, numar.parteReala, numar.parteFractionara);
      printf(&quot;\n&quot;);
  }
  
  void citireNumar (struct numarComplex *numar) {
      printf (&quot;Introduceti partea reala: &quot;);
      scanf(&quot;%d&quot;, &amp;numar -&gt; parteReala);
      printf (&quot;Introduceti partea fractionara: &quot;);
      scanf(&quot;%d&quot;, &amp;numar -&gt; parteFractionara);
  }
  
  void scriereNumarTxt (struct numarComplex numar) {
      // am decis ca la alegerea data sa se scrie in fisier .txt, sa se utilizeze numerele introduse de la tastatura in 1, pentru comoditate
      FILE *nrTxt;
      nrTxt = fopen(&quot;./numarcomplex.txt&quot;, &quot;w+&quot;);
      fprintf(nrTxt, &quot;%d.%d\n&quot;, numar.parteReala, numar.parteFractionara);
      fclose(nrTxt);
  }
  
  int main()
  {
      int optiune;
      do {
          printf(&quot;Meniu: \n&quot;);
          printf(&quot;1. Citire de la tastatura: \n&quot;);
          printf(&quot;2. Afisare la ecran: \n&quot;);
          printf(&quot;3. Citire din fisier text: \n&quot;);
          printf(&quot;4. Scriere in fisier text: \n&quot;);
          printf(&quot;5. Citire din fisier binar: \n&quot;);
          printf(&quot;6. Scriere in fisier binar: \n&quot;);
          printf(&quot;7. Iesire: &quot;);
          scanf(&quot;%d&quot;, &amp;optiune);
          switch (optiune) {
          case 1: {for (int i = 0; i &lt; 5; i++) {
                  citireNumar(&amp;numere[i]);
              }
              break;
          }
          case 2: {
              printf(&quot;Tabloul de structuri: \n&quot;);
              for (int i = 0; i &lt; 5; i++) {
                  printareNumar(numere[i]);
              }
              break;
          }
          case 4: {
              for (int i = 0; i &lt; 5; i++) {
                  scriereNumarTxt(numere[i]);
              } break;
          }
          }
      } while (optiune != 7);
  
  }
  </code></pre>