#include <stdio.h>
#include <time.h>
#include <unistd.h>
#include <string.h>
int main(int argc, const char *argv[])
{
FILE*fp = fopen("time.txt","a+");
if(NULL==fp)
{
perror("fopen");
return -1;
}
char buf[32]="";
int count=0;
while(1)
{
if(fgets(buf,sizeof(buf),fp)==NULL)
break;
if('\n'==buf[strlen(buf)-1])
count++;
}
if(buf[strlen(buf)-1]!='\n')
count++;
time_t now;
struct tm *tms;
while(1)
{
now=time(0);
tms=localtime(&now);
fprintf(fp,"[%d]",count);
fprintf(fp,"%4d-%2d-%2d %2d:%2d:%2d\n",tms->tm_year+1900,\
tms->tm_mon+1,\
tms->tm_mday,\
tms->tm_hour,\
tms->tm_min,\
tms->tm_sec);
fflush(fp);
count++;
sleep(1);
}
fclose(fp);
return 0;
}