/* Copyright 2014 by R. Harmsen.

   For a full explanation see
   https://rudhar.com/sfreview/truncexp/en.htm
 */
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

char *logfilename = "logfile";

int main (int argc, char **argv)
{
   long int i;
   FILE *fp;
   int ret, charsperline = 60;

   if ((fp = fopen(logfilename, /* "w" */ "a")) == NULL)
   {
      fprintf(stderr, "Could not open %s for writing\n", logfilename);
      perror(NULL);
      exit(1);
   }

   for (i = 0; i < 600; i++)
   {
      if ((ret = fprintf(fp, "Writing a line to the logfile, count = %5ld (calc'd: %5ld), position = %9ld\n",
                    (long)i, (long)(ftell(fp) / charsperline), (long)ftell(fp))) < 0)
      {
          fprintf(stderr, "Fprintf failed, ret = %d\n", ret);
          perror(NULL);
      }
      else
      {
         charsperline = ret;
      }
   }
   fflush(fp);

   for (; i < 600 + 100 * 60; i++)
   {
      #ifdef DEBUG
      fprintf(stderr, "Writing a line to the logfile, count = %5ld (calc'd: %5ld), position = %9ld\n",
                    (long)i, (long)(ftell(fp) / charsperline), (long)ftell(fp));
      #endif

      if ((ret = fprintf(fp, "Writing a line to the logfile, count = %5ld (calc'd: %5ld), position = %9ld\n",
                    (long)i, (long)(ftell(fp) / charsperline), (long)ftell(fp))) < 0)
      {
          fprintf(stderr, "Fprintf failed, ret = %d\n", ret);
          perror(NULL);
      }
      fflush(fp);
      sleep(10);
   }

   return 0;
}

/* Copyright 2014 by R. Harmsen.

   For a full explanation see
   https://rudhar.com/sfreview/truncexp/en.htm
 */