在已写时间的文件中继续追加时间记录
#include
#include
#include
#include
#define BUFF 50
int main()
{
FILE *fp;
int count = 0;
char buf[BUFF];
time_t t;
struct tm *ts;
if ((fp = fopen("time.txt", "r+")) == NULL)
{
perror("open error");
return -1;
}
while (fgets(buf, BUFF, fp) != NULL)
{
count++;
}
while(1)
{
time(&t);
ts = localtime(&t);
fprintf(fp, "%d,%d-%d-%d %d:%d:%d\n", ++count, ts->tm_year+1900, ts->tm_mon+1, ts->tm_mday, ts->tm_hour, ts->tm_min, ts->tm_sec);
fflush(fp);
sleep(1);
}
return 0;
}
阅读(991) | 评论(0) | 转发(2) |