#include <stdio.h>
#include <time.h>
#include <unistd.h>
#include <string.h>
int main()
{
FILE *fp;
fp = fopen("time_log.txt", "a+");
time_t ctime;
struct tm *mlocaltime;
long long int n = 1;
char buff[24];
fseek(fp, 0, SEEK_SET);
while (fread(buff, sizeof(char), 24, fp) != 0)
{
n++;
}
while (1)
{
ctime = time(NULL);
mlocaltime = localtime(&ctime);
printf("%d-%d-%d %d:%d:%d\n", mlocaltime->tm_year + 1900, mlocaltime->tm_mon + 1, mlocaltime->tm_mday, mlocaltime->tm_hour, mlocaltime->tm_min, mlocaltime->tm_sec);
fprintf(fp, "%d\t", n);
fprintf(fp, "%d-%d-%d %d:%d:%d\n", mlocaltime->tm_year + 1900, mlocaltime->tm_mon + 1, mlocaltime->tm_mday, mlocaltime->tm_hour, mlocaltime->tm_min, mlocaltime->tm_sec);
sleep(1);
// fflush(fp);
n++;
}
}
![img](https://img-mid.csdnimg.cn/release/static/image/mid/ask/825917138186134.png?x-oss-process=image/auto-orient,1/resize,w_320,m_lfit)