//向某文件中逐秒追加带序号输入当前时间
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<string.h>
#include <unistd.h>
int main(int argc, char const *argv[])
{
time_t tv;
// time(&tv);//法1:获取秒数
// tv=time(NULL);//法2
// tv=time(&tv);//法3
// printf("%d",tv);
struct tm *timenow;//将time获取的秒数转化为年月日时分秒
char *timec = (char *)malloc(100);
int i=1;
FILE *fp = fopen(argv[1],"a+");
if(NULL == fp)
{
perror("fopen err");
return -1;
}
fprintf(fp,"\n");
while (1)
{
time(&tv);//法1:获取秒数
timenow = localtime(&tv);//将time获取的秒数转化为年月日时分秒
// sprintf(timec,"%d-%d-%d,%d:%d:%d",timenow->tm_year+1900,timenow->tm_mon+1,
// timenow->tm_mday,timenow->tm_hour,timenow->tm_min,timenow->tm_sec);//sprintf是将数据写入到字符串中
fprintf(fp,"%-4d",i);
sleep(1);
// fputs(timec,fp);
fprintf(fp,"%d-%d-%d,%d:%d:%d",timenow->tm_year+1900,timenow->tm_mon+1,
timenow->tm_mday,timenow->tm_hour,timenow->tm_min,timenow->tm_sec);
fflush(NULL);
fputc('\n',fp);
i++;
}
return 0;
}