title: C Programming - Copy File Buffer and Write to File
body: <p>I'm making some small changes to a preexisting application.  The app is writing lines of text to a buffer and then flushing the buffer.  I'm not sure when it actually writes the text to the file, but I'm trying to copy everything in that buffer and write a copy of it all out to a completely different file.</p>
  
  <p>Below is the last line of writing to the preexisting buffer before it eventually calls <code>fflush()</code>.</p>
  
  <pre><code>fprintf(_log, "-- FINAL TEXT LINE --\n");
  </code></pre>
  
  <p>Below is my code that I'm using in an attempt to copy that buffer to a separate file which is dynamically named according to the log time. This <code>custom-%ld.log</code> does not already exist and needs to be created.</p>
  
  <pre><code>char tmp[sizeof(_log)];
  
  sprintf(tmp, "custom-%ld.log", (long int)lf-&gt;time);
  
  FILE *fp1, *fp2;
  char a;
  
  fp1 = _log;
  
  fp2 = fopen(tmp, "a");
  if (fp2 != NULL) {
  
      do {
        a = fgetc(fp1);
        fputc(a, fp2);
      } while (a != EOF);
  
      fflush(fp1);
      fclose(fp2);
  }
  
  fflush(_log);
  </code></pre>
  
  <p>I'm sure my mistakes are very basic, but I don't know what I'm doing.  I've tried dozens of suggestions on other websites, and suggestions here from other questions, but I'm not having any luck.</p>