#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <unistd.h>
int line(FILE * fp);
int main(int argc, const char *argv[])
{
FILE *fp=fopen("time.txt","a+");
if(fp==NULL)
{
perror("fopen");
}
struct tm *p=NULL;
int count;
int i=1;
while(1)
{
count=line(fp);
time_t t=time(NULL);
p=localtime(&t);
fprintf(fp,"[%d] %d-%02d-%02d %02d:%02d:%02d",count+i,p->tm_year+1900,p->tm_mon+1,p->tm_mday,p->tm_hour,p->tm_min,p->tm_sec);
fflush(fp);
fputc('\n',fp);
sleep(1);
i++;
}
fclose(fp);
return 0;
}
int line(FILE * fp)
{
int count=0;
char c;
while(1)
{
c=fgetc(fp);
if(c==EOF)
{
perror("fgetc");
break;
}
if(c=='\n')
{
count++;
}
}
return count;
}