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