问题
通过 fwrite和 fread去拷贝 文件到另外一个文件上
#include<myhead.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include<errno.h>
#include<time.h>
int main(int argc, const char *argv[])
{
FILE *fp =fopen(argv[1],"r");
if(fp==NULL)
{
perror("-1");
return 0;}
char arr[100]=" ";
int res=0;
FILE *op=fopen(argv[2],"w+");
if(op==NULL)
{perror("-1");
return -1;}
while((res=fread(arr,1,sizeof(arr),fp))>0)
{
fwrite(arr,1,res,op);}}
向文件中每一秒打印当前系统时间
include<myhead.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include<errno.h>
#include<time.h>
typedef struct times
{
int year;
int month;
int day;
int hour;
int min;
int sec;
int d;
} ss;
int getss(FILE*fp)
{
char b[256]=" ";
int a=0;
while(fgets(b,sizeof(b),fp)!=NULL)
{sscanf(b,"%d",&a);
}
return a;
}
int main(int argc, const char *argv[])
{time_t sys_time =0;int c=0;int a=-1;FILE *fp=fopen("suhui.txt","r");
if(fp==NULL)
{fp=fopen("suhui.txt","w+");}else
{ c=getss(fp);fclose(fp);
fp=fopen("suhui.txt","w+");}if(fp==NULL){perror("-1");}if(c==0){c=1;}else{c++;}
while(1)
{ time(&sys_time);ss s;struct tm *tm_ptr=localtime(&sys_time);
s.year=tm_ptr->tm_year+1900;
s.month=tm_ptr->tm_mon+1;
s.day= tm_ptr->tm_mday;
s.hour=tm_ptr->tm_hour;
s.min=tm_ptr->tm_min;
s.sec=tm_ptr->tm_sec;
if(s.sec!=a)
{fprintf(fp," %d.0 %4d-%2d-%2d %2d:%2d:%2d\n ",c,s.year,s.month,s.day,s.hour,s.min,s.sec );
fflush(fp);
c++;}a=s.sec;
}
fclose(fp);
return 0;}
3. fgets去计算\n
#include<myhead.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include<errno.h>
#include<time.h>
int main(int argc, const char *argv[])
{
FILE *fp =fopen(argv[1],"r");
if(fp==NULL)
{
perror("-1");
return 0;}
char arr[100]=" ";
int res=0;
FILE *op=fopen(argv[2],"w+");
if(op==NULL)
{perror("-1");
return -1;}
while((res=fread(arr,1,sizeof(arr),fp))>0)
{
fwrite(arr,1,res,op);}
fclose(fp);
fclose(op);
fp=fopen(argv[2],"r");
char b[20]=" ";int h=0;
while(1)
{char *t=fgets(b,sizeof(b),fp);
if(t==NULL)
{break;}
if(b[strlen(b)-1]=='\n')
{
h++;}}printf("%d",h);}