第一题
main.c
#include "8.21z.h"
int main(int argc, const char *argv[])
{
int ch;
while(1)
{
printf("\t\t\t1.注册\n");
printf("\t\t\t1.登录\n");
printf("\t\t\t1.退出\n");
printf("请输入您的选择:");
scanf("%d",&ch);
getchar();
switch(ch)
{
case 1:
regist();
break;
case 2:
login();
break;
case 3:
exit(EXIT_SUCCESS);
break;
}
}
return 0;
}
1.c
#include "8.21z.h"
int regist()
{
user reg;
printf("请输入您需要注册的账户:");
fgets(reg.name,sizeof(reg.name),stdin);
printf("请输入您需要注册的密码:");
fgets(reg.code,sizeof(reg.code),stdin);
FILE *fp = fopen("./user.txt","w");
if(fp==NULL)
{
perror("fopen");
return -1;
}
fprintf(fp,"%s%s",reg.name,reg.code);
printf("注册成功\n");
fclose(fp);
}
2.c
#include "8.21z.h"
int login()
{
user log,reg;
printf("请输入您的账户:");
fgets(log.name,sizeof(log.name),stdin);
printf("请输入您的密码:");
fgets(log.code,sizeof(log.code),stdin);
//log.name[strlen(log.name)-1] ='\0';
//log.code[strlen(log.code)-1] ='\0';
printf("%s|%s\n",log.name,log.code);
FILE *fp = fopen("./user.txt","r");
if(fp==NULL)
{
perror("fopen");
return -1;
}
fscanf(fp,"%s%s",reg.name,reg.code);
reg.name[strlen(reg.name)+1] = '\0';
reg.name[strlen(reg.name)] = '\n';
reg.code[strlen(reg.code)] = '\n';
printf("%s|%s",reg.name,reg.code);
if(strcmp(reg.name,log.name)==0 &&
strcmp(reg.code,log.code)==0)
{
printf("登陆成功\n");
}
fclose(fp);
}
.h
#ifndef __Z_H__
#define __Z_H__
#include <myhead.h>
typedef struct
{
char name[20];
char code[20];
}user;
int login();
int regist();
#endif
第二题
#include <myhead.h>
int main(int argc, const char *argv[])
{
FILE *fp1 = fopen("./1.txt","r");
if(fp1==NULL)
{
perror("fopen");
return -1;
}
FILE *fp2 = fopen("./2.txt","w");
if(fp2==NULL)
{
perror("fopen");
return -1;
}
int i=1;
char buf[100]={0};
int len = fread(buf,1,sizeof(buf),fp1);
//printf("%d",len);
fwrite(buf,1,len,fp2);
fclose(fp1);
fclose(fp2);
return 0;
}