title: Write and Read at the Same Time - C
body: <p>This is a sketch how I want to write a .txt:</p>
  
  <pre><code>time_t timer;
  struct tm y2k = { 0 };
  int seconds;
  int seconds_prev= 0;
  int i = 0;
  y2k.tm_hour = 0;   y2k.tm_min = 0; y2k.tm_sec = 0;
  y2k.tm_year = 100; y2k.tm_mon = 0; y2k.tm_mday = 1;
  
  FILE *f = fopen("file.txt", "w");
  
  if (f == NULL)
  {
      printf("Error opening file!\n");
      return 0;
  }
  
  for (;;){
  
      time(&amp;timer);   
      seconds = difftime(timer, mktime(&amp;y2k));
  
      if (seconds % 2 == 0 &amp;&amp; seconds!=seconds_prev) {
          seconds_prev= seconds;
          const char *text = "AAAAAA";
          fprintf(f, "%d-%s\n",i, text);
          fflush(f);
          i++
          if (i == 20){
              break;
          }
      }
  }
  
  fclose(f);
  </code></pre>
  
  <p>This script enable writing roughly 1 line per second in a text file.
  Now I want to read this file until it is complete.</p>
  
  <pre><code>FILE *file;
  size_t nread;
  
  file = fopen("file.txt", "r");
  if (file) {
      /*pseudo code*/
      //(reading loop)
      //(some condition that detects the true end of file writing)
      //(buffering information)
      //(end of loop)
      /**************/
  
      if (ferror(file)) {
          /* deal with error */
      }
      fclose(file);
  }
  </code></pre>
  
  <p>So the main objective is to read the "file.txt" while it is been writing. The big problem that I face, is the fact that the script only reads the information already written and stops the reading. I am searching some condition, flag, etc that me let resolve this problem. So far, nothing</p>