1、完成标准io的单字符、字符串、格式化、模块化实现两个文件的拷贝;
#include <myhead.h>
typedef struct sockaddr_in addr_in_t;
typedef struct sockaddr addr_t;
typedef struct sockaddr_un addr_un_t;
int main(int argc, const char *argv[])
{
FILE* rfp = fopen(argv[1],"r");
if(NULL == rfp){
perror("fopen err:");
return -1;
}
//字符拷贝
FILE* wfp = fopen("zifu.c","w");
if(NULL == wfp){
perror("zifufopen err:");
return -1;
}
printf("0\n");
while(1){
char ch = fgetc(rfp);
if(ch == EOF)break;
fputc(ch,wfp);
}
printf("1\n");
//字符串拷贝
FILE* ps = fopen("zifuchuan.c","w");
char arr[12] = "";
if(NULL == ps){
perror("zifuchuanfopen err:");
return -1;
}
fseek(rfp,0,SEEK_SET);
while(1){
char*res = fgets(arr,sizeof(arr),rfp);
if(res == NULL)break;
fputs(arr,ps);
}
printf("2\n");
//格式拷贝
FILE* pf = fopen("geshi.c","w");
if(NULL == pf){
perror("geshifopen err:");
return -1;
}
fseek(rfp,0,SEEK_SET);
while(1){
char ch;
int s = fscanf(rfp,"%c",&ch);
if(s==EOF)break;
fprintf(pf,"%c",ch);
}
printf("3\n");
//Mo_Kuai拷贝
FILE* fr = fopen("mokuai.c","w");
if(NULL == pf){
perror("mokuaifopen err:");
return -1;
}
fseek(rfp,0,SEEK_SET);
while(1){
int res = fread(arr,sizeof(arr),1,rfp);
// if(res == )break;
if(feof(rfp))break;
fwrite(arr,sizeof(arr),1,fr);
}
fclose(fr);
fclose(rfp);
fclose(wfp);
fclose(ps);
fclose(pf);
return 0;
}
2、实现注册和登录功能,将注册账号和密码存储到文件中,登录时遍历文件如果存在就登录成功否则登录失败
#include <myhead.h>
typedef struct sockaddr_in addr_in_t;
typedef struct sockaddr addr_t;
typedef struct sockaddr_un addr_un_t;
typedef struct {//定义账号密码结构体
char zhanghao[20];
char mima[20];
}deng_lu;
int main(int argc, const char *argv[])
{
FILE* zfd = fopen("shujuku.txt","a");
FILE* dfd = fopen("shujuku.txt","r");
deng_lu yong_hu,shu_ju;//用于接受读取文件中的用户密码
while(1){
printf("请选择操作\n");
printf("\t1、登录\n");
printf("\t2、注册\n");
printf("\t3 tuichu\n");
int a;
scanf("%d",&a);
if(a==2){ //注册功能
printf("请输入账号和密码中间以空格隔开\n");
scanf("%s %s",yong_hu.zhanghao,yong_hu.mima);
fseek(dfd,0,SEEK_SET);
int t=1;
while(!feof(dfd)){
fread(&shu_ju,sizeof(deng_lu),1,dfd);
if(strcmp(shu_ju.zhanghao,yong_hu.zhanghao)==0){
printf("用户已存在\n");t=0;break;
}}
if(t==1){
fwrite(&yong_hu,sizeof(deng_lu),1,zfd);
fflush(zfd);
printf("注册成功\n");
}}
else if(a==1){ //登录功能
//fread(&yong_hu,sizeof(deng_lu),1,0);
printf("请输入账号和密码中间以空格隔开\n");
scanf("%s %s",yong_hu.zhanghao,yong_hu.mima);
int s=0;
fseek(dfd,0,SEEK_SET);
while(1){
int c = fread(&shu_ju,sizeof(deng_lu),1,dfd);
//printf("%s %s\n",shu_ju.zhanghao,shu_ju.mima);
if(c==0)break;
if(strcmp(shu_ju.zhanghao,yong_hu.zhanghao)==0&&strcmp(shu_ju.mima,yong_hu.mima)==0){
printf("登陆成功\n");
s=1;break;
}}
if(s==0){printf("用户不存在或密码错误\n");}
}
else if(a==3){
return 0;
}
else{
printf("不存在该功能\n");
}
}
fclose(zfd);
fclose(dfd);
return 0;
}