#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main(int argc, const char *argv[])
{
FILE *fp;
struct tm *tm_t;
time_t t;
int ch;
int n = 1;
if ((fp = fopen("localtime.c","a+")) == NULL)
{
perror ("fail to fopen");
exit (1);
}
while (1)
{
while ((ch = fgetc (fp)) != EOF)
{
if (ch == '\n')
n++;
}
time (&t);
tm_t = localtime (&t);
fprintf(fp,"%d %d-%d-%d %d:%d:%d\n",n++,tm_t->tm_year+1900,tm_t->tm_mon+1,tm_t->tm_mday,tm_t->tm_hour,tm_t->tm_min,tm_t->tm_sec);
fflush (fp);
sleep (1);
}
fclose (fp);
return 0;
}
编程读写一个文件test.txt,每隔1秒向文件中写入一行数据
最新推荐文章于 2023-07-29 10:01:31 发布