title: Redirect console output to the end of a file C++ using ofstream
body: <p>I want to add new out put from a buff to the end of a file <code>rst.txt</code> using ofstream.
  My problem is that every new entry erases the file containt.</p>
  
  <pre><code> #include &lt;iostream&gt;
      #include &lt;sstream&gt;
      #include &lt;stdio.h&gt;
      #include &lt;fstream&gt;
  
      using namespace std;
  
      int main()
      {
          FILE *in;
  
  
          char buff[512];
  
  
  
  
          while(1)
          {
              //if(!(in = popen("tcpdump -i wlan0 -e -c 1 -vvv /home/pi/wifi", "r")))
              if(!(in = popen(" sudo tcpdump -i wlan0 -e -c 1 'type mgt subtype probe-req' -vvv", "r")))
                  return 1;
  
              fgets(buff, sizeof(buff), in);
  
              pclose(in);
  
              std::istringstream iss;
  
              iss.str(buff);
  
              std::string mac_address;
              std::string signal_strength;
  
              std::string info;
              while(iss &gt;&gt; info)
              {
          if( info.find("SA") != std::string::npos )
                      mac_address = info.substr(3);
  
                  if( info.find("dB") != std::string::npos )
                      signal_strength = info;
  
  
              }
              ofstream file;
              file.open("rst.txt");
              streambuf* sbuf=cout.rdbuf();
              cout.rdbuf(file.rdbuf());
              cout&lt;&lt;"file"&lt;&lt; endl;
  
              cout &lt;&lt; "      ADRESSE MAC     : " &lt;&lt; mac_address &lt;&lt; "     " ;
              cout &lt;&lt; "      SIGNAL STRENGTH : " &lt;&lt; signal_strength &lt;&lt; "  " &lt;&lt; endl;
          }
  
           return 0;
      } 
  </code></pre>
  
  <p>Is there a way to redirect the output to the end of a file ?
  Is there any way better than ofstream ?</p>