无法实现按ctrl + c 停止 ,再次运行该程序,继续之前的行号,往里面写下时间
/*1: 编写一个程序,每次运行该程序每隔1秒就往同一个文件中写入当前的时间
要求有行号
1 Sat Jan 30 00:33:36 PST 2016
2 Sat Jan 30 00:33:37 PST 2016
3 Sat Jan 30 00:33:38 PST 2016
按ctrl + c 停止
再次运行该程序,继续之前的行号,往里面写下时间
4 Sat Jan 30 00:40:38 PST 2016
5 Sat Jan 30 00:40:39 PST 2016
*/
# include <stdio.h>
# include <stdlib.h>
# include <time.h>
# include <string.h>
time_t result;
char * data ;
FILE*fp;
FILE*lfp;
int count;
char buf [100];
//char lbuf [10];
int main (void)
{
fp = fopen ("./a.txt","a");
if ( fp == NULL)
{
perror ("fopen failed");
return -1;
}
lfp = fopen ("./lin.txt","a+");
if (lfp == NULL)
{
perror ("fopen failed");
return -1;
}
lfp = freopen("./lin", "w+", lfp);
//printf ("%d\n",count);
while (1)
{
//bzero (buf,100);//清空buf
count++;
result = time (NULL);
data = asctime( localtime(&result));
sprintf(buf,"%d.%s",count,data);
fwrite (buf,strlen(buf),1,fp);//将buf里的写入fp的文件里
fprintf(lfp, "%d\n", count);
printf ("%s",buf);
fseek(lfp, 0 , SEEK_SET);
fflush (lfp);
fflush (fp);//强退还能将buf里的存到fp的文件里
sleep (1);
}
exit(0);
}