1>
#include<myhead.h>
int count(FILE *stream)
{
char buf[10];
int line=1;
while(fgets(buf,sizeof(buf),stream)!=NULL)
{
if(buf[strlen(buf)-1]=='\n')
{
line++;
}
}
return line;
}
int main(int argc, const char *argv[])
{
int i,num=-1;
while(1)
{
time_t systime=time(NULL);
struct tm *tm_ptr=localtime(&systime);
FILE *fp;
if((fp=fopen("./aa.c","a+"))==NULL)
{
perror("fopen error");
return -1;
}
if(tm_ptr->tm_sec != num)
{
i=count(fp);
fprintf(fp,"%2d、%2d:%2d:%2d\n",i,tm_ptr->tm_hour,\
tm_ptr->tm_min,\
tm_ptr->tm_sec);
i++;
fflush(fp);
}
num=tm_ptr->tm_sec;
fclose(fp);
}
return 0;
}
2、使用fread、fwrite完成两个文件的拷贝
#include<myhead.h>
int main(int argc, const char *argv[])
{
if(argc!=3)
{
printf("usage:./a.out argv[1] argv[2]\n");
return -1;
}
FILE *fp,*fp1;
if((fp=fopen(argv[1],"r"))==NULL)
{
perror("fp open error");
return -1;
}
if((fp1=fopen(argv[2],"w"))==NULL)
{
perror("fp1 open error");
return -1;
}
char num[125];
int ret;
while(!feof(fp))
{
ret=fread(num,1,sizeof(num),fp);
fwrite(num,1,ret,fp1);
}
fclose(fp);
fclose(fp1);
printf("拷贝成功\n");
return 0;
}