title: How to append file in c++ using fstream?
body: <p>I try to append file in C++. At start file doesn't exists. After operations there is only one line in file instead of five (5 calls of this method). It looks like file is creating, next every write operation file is cleaned out and new string is added.</p>
  
  <pre><code>void storeUIDL(char *uidl) {
          fstream uidlFile(uidlFilename, fstream::app | fstream::ate);
  
          if (uidlFile.is_open()) {
          uidlFile &lt;&lt; uidl;
          uidlFile.close();
      } else {
          cout &lt;&lt; "Cannot open file";
      }
  }
  </code></pre>
  
  <p>I tried with <code>fstream::in ,fstream::out</code>. How to append string correctly in this file?</p>
  
  <p>Thank you in advance.</p>
  
  <p>edit:</p>
  
  <p>Here is wider point of view:</p>
  
  <pre><code>for (int i = 0; i &lt; items; i++) {
      MailInfo info = mails[i];
      cout &lt;&lt; "Downloading UIDL for email " &lt;&lt; info.index &lt;&lt; endl;
  
      char *uidl = new char[100];
      memset(uidl, 0, 100);
      uidl = servicePOP3.UIDL(info.index);
      if (uidl != NULL) {
          if (existsUIDL(uidl) == false) {
              cout &lt;&lt; "Downloading mail with index " &lt;&lt; info.index &lt;&lt; endl;
              char *content = servicePOP3.RETR(info);
  
              /// save mail to file
              string filename = string("mail_" + string(uidl) + ".eml");
              saveBufferToFile(content, filename.c_str());
              storeUIDL(uidl);
              sleep(1);
          } else {
              cout &lt;&lt; "Mail already exists." &lt;&lt; endl;
          }
      } else {
          cout &lt;&lt; "UIDL for email " &lt;&lt; info.index &lt;&lt; " does not exists";
      }
  
      memset(uidl, 0, 100);
      sleep(1);
  }
  </code></pre>