标准创建文件将实时时间打印到文件并添加序号:
#include<stdio.h>
#include <time.h>
#include <stdlib.h>
#include <unistd.h>
int main(int argc, const char *argv[])
{
FILE* fp=fopen("./time.txt","a+");
if(NULL ==fp)
{
perror("fopen");
return -1;
}
char c;
int k=1;
while(1)
{
c=fgetc(fp);
if(EOF == c)
break;
else if('\n' == c)
k++;
}
int m=k/2+1;
time_t now;
struct tm *p=NULL;
//system("clean"); system让代码执行shell指令
while(1)
{
now=time(NULL);
p =localtime(&now);
fprintf(fp,"[%d] %4d-%02d-%02d %02d-%02d-%02d\n\n",\
m,p->tm_year+1900,p->tm_mon+1,p->tm_mday,\
p->tm_hour,p->tm_min,p->tm_sec);
m++;
fflush(fp);
sleep(1);
}
fclose(fp);
return 0;
}