#include<myhead.h>
#include<time.h>
int line_return(FILE *p)
{
char buf[10];
int line=0;
while(fgets(buf,sizeof(buf),p)!=NULL)
{
if(buf[strlen(buf)-1]=='\n')
{
line++;
}
}
return line;
}
int main(int argc, const char *argv[])
{
time_t newtime,oldtime;
//定义一个系统时间变量
time_t sysTime = time(NULL);
//定义时间结构体指针,执行时间
struct tm *tm_ptr = localtime(&sysTime);
FILE *fp;
fp=fopen("bbb.txt","a");
if(fp==NULL)
{
perror("open error");
return -1;
}
int line;
line=line_return(fp);
while(1)
{
newtime=time(NULL);
tm_ptr=localtime(&newtime);
if(newtime!=oldtime)
{
oldtime=newtime;
fprintf(fp,"%3d.%02d:%02d:%02d\n",\
++line,\
tm_ptr->tm_hour,\
tm_ptr->tm_min,\
tm_ptr->tm_sec);
printf("\n");
fflush(fp);
}
}
fclose(fp);
return 0;
}
2、使用fread、fwrite完成两个文件的拷贝
#include <myhead.h>
int main(int argc, const char *argv[])
{
char buf[10];
FILE *srcfp=fopen("./1test.c","r");
FILE *dstfp=fopen("aaa.txt","w");
if(srcfp==NULL)
{
perror("open error");
return -1;
}
while(1)
{
memset(buf,0,sizeof(buf));
fread(buf,sizeof(buf[0]),sizeof(buf),srcfp);
fwrite(buf,sizeof(buf[0]),sizeof(buf),dstfp);
if(feof(srcfp))
{
printf("读取完成\n");
break;
}
if(ferror(srcfp))
{
printf("读取失败\n");
break;
}
}
return 0;
}