使用fread、fwrite完成文件拷贝
#include <myhead.h>
int main(int argc, const char *argv[])
{
if(argc!=3)
{
printf("参数有误");
}
//定义并以只写的方式打开两个文件
FILE *fp=NULL;
FILE *cfp=NULL;
if((fp=fopen(argv[1],"w"))==NULL)
{
perror("fopen error");
return -1;
}
if((cfp=fopen(argv[2],"w"))==NULL)
{
perror("fopen error");
return -1;
}
//终端输入,写入文件
char buf[128]="";
fgets(buf,sizeof(buf),stdin);
buf[sizeof(buf)-1]='\0';
fwrite(buf,strlen(buf),1,fp);
fclose(fp);
fclose(cfp);
if((fp=fopen(argv[1],"r"))==NULL)
{
perror("fopen error");
return -1;
}
if((cfp=fopen(argv[2],"w"))==NULL)
{
perror("fopen error");
return -1;
}
//从fp中读取数据拷贝到cfp中
char rbuf[128]="";
int res=fread(rbuf,1,sizeof(rbuf),fp);
fwrite(rbuf,1,res,cfp);
fclose(fp);
fclose(cfp);
return 0;
}
?
注册登录的框架
#include <myhead.h>
int zhuce()
{
FILE *fp=NULL;
if((fp=fopen("./test.txt","a+"))==NULL)
{
perror("fopen error");
return -1;
}
char zzh[20]="";
char zmm[20]="";
printf("请输入注册账号:");
scanf("%s",zzh);
printf("请输入注册密码:");
scanf("%s",zmm);
fprintf(fp,"%s %s\n",zzh,zmm);
printf("注册成功\n");
fclose(fp);
}
int denglu()
{
FILE *fp=NULL;
if((fp=fopen("./test.txt","a+"))==NULL)
{
perror("fopen error");
return -1;
}
char dzh[20]="";
char dmm[20]="";
char zzh[20]="";
char zmm[20]="";
printf("请输入账号:");
scanf("%s",dzh);
printf("请输入密码:");
scanf("%s",dmm);
while(1)//循环进行对以储存的账号密码比较
{
fscanf(fp,"%s %s\n",zzh,zmm);
if(strcmp(dzh,zzh)==0&&strcmp(dmm,zmm)==0)
{
printf("登陆成功\n");
break;
}
}
}
int main(int argc, const char *argv[])
{
int num;
while(1)
{
printf("\t\t***********1.注册************\n");
printf("\t\t***********2.登录************\n");
printf("\t\t***********3.退出************\n");
printf("请输入选项:");
scanf("%d",&num);
switch(num)
{
case 1:
{
zhuce();
}
break;
case 2:
{
denglu();
}
break;
case 3:
exit(EXIT_SUCCESS);
default:printf("输入有误,请重新输入:");
}
while(getchar()!='\n');//循环吸收垃圾字符
printf("按任意键清屏:");
while(getchar()!='\n');
system("clear");//调用指令对终端清屏
}
return 0;
}
?
?
图像信息的读写操作
#include <myhead.h>
int main(int argc, const char *argv[])
{
FILE *fp=NULL;
if((fp=fopen("./helltaker.bmp","r+"))==NULL)
{
perror("fopen error");
return -1;
}
unsigned char color[3]={255,0,0};
fseek(fp,54,SEEK_SET);
for(int i=0;i<200;i++)
{
for(int j=0;j<1920;j++)
{
fwrite(color,sizeof(color),1,fp);
}
}
unsigned char coloq[3]={200,0,0};
fseek(fp,54,SEEK_SET);
for(int i=0;i<150;i++)
{
for(int j=0;j<1920;j++)
{
fwrite(coloq,sizeof(coloq),1,fp);
}
}
unsigned char colow[3]={150,0,0};
fseek(fp,54,SEEK_SET);
for(int i=0;i<100;i++)
{
for(int j=0;j<1920;j++)
{
fwrite(colow,sizeof(colow),1,fp);
}
}
unsigned char coloe[3]={100,0,0};
fseek(fp,54,SEEK_SET);
for(int i=0;i<50;i++)
{
for(int j=0;j<1920;j++)
{
fwrite(coloe,sizeof(coloe),1,fp);
}
}
fclose(fp);
return 0;
}
?
?