c - Write Data to File Periodically -


i have written code in c write data text file periodically, program won't write periodically; writes file without waiting. want write data file, wait ~2 seconds, write next line.

for (i = 0; < 8; i++) {     tm.hh = random() % 23;     tm.mm = random() % 59;     tm.ss = random() % 59;      dt.dd = random() % 31;     dt.mm = random() % 12;     dt.yy = random() % 2020;     if (dt.yy < 2020 && dt.yy > 2015)     {         while (dt.yy < 1900)         {             dt.yy = random() % 2020;         }     }      val1 = random() % 100;     val2 = random() % 100;      fprintf(fp, "%d:%d:%d,%d/%d/%d,%d,%d,\n", tm.hh, tm.mm, tm.ss, dt.dd, dt.mm, dt.yy, val1, val2);     id = fork();     if (id == -1)         printf("error in creating child process\n");     else if (id == 0)     {         exit(0);     }     else         wait(null);  } fclose(fp); 

you may use sleep() function right before writing line. edit: need include:

in windows

#include <windows.h>   sleep(<timeinms>); 

in unix

#include <unistd.h> sleep(<timeinseconds>); 

Comments