回复 10楼 拉链
是啊,忘了把文件里的内容重新读出来了,改过后的# 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","a+");
if (lfp == NULL)
{
perror ("fopen failed");
return -1;
}
fread (lbuf,5,2,lfp);//将lfp文件的内容读到buf
count = atoi(lbuf);//把buf中的字符转换成整数
lfp = freopen("./lin", "w", lfp);
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);
}