#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <time.h>
#include <unistd.h>
#include <stdlib.h>
int txt_line(FILE* fp1);
int txt_line(FILE* fp1)
{
fp1 =fopen("./time.txt","r");
if(fp1==NULL)
{
perror("fopen");
return -1;
}
char str[20]={0};
int line=1;
while(1)
{
//bzero(str,sizeof(str));
if(fgets(str,sizeof(str),fp1)==NULL)
{
break;
}
if(str[strlen(str)-1]=='\n')
{
line++;
}
}
fclose(fp1);
return line;
}
int main(int argc, const char *argv[])
{
FILE *fp=fopen("./time.txt","a+");
{
if(NULL==fp)
{
perror("fopen");
return -1;
}
}
int line=txt_line(fp);
time_t t;
struct tm *info=NULL;
while(1)
{
// system("clear");
t=time(NULL);
info=localtime(&t);
fprintf(fp,"[%2d]%d-%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);
fflush(stdout);
sleep(1);
}
return 0;
}