1,思维导图
2,登录
#ifndef __LOG_H__
#define __LOG_H__
#include<myhead.h>
typedef struct
{
char name[20];
char pwd[20];
}str;
int regist();
int login();
#endif
#include"log.h"
int login()
{
char a[20]="\n";
str p,s;
printf("输入登录账户:");
fgets(p.name,sizeof(p.name),stdin);
printf("输入登录密码:");
fgets(p.pwd,sizeof(p.pwd),stdin);
printf("%ld%ld",strlen(p.name),strlen(p.pwd));
// p.name[strcspn(p.name,"\n")]='\0';
// p.pwd[strcspn(p.pwd,"\n")]='\0';
FILE *fp=fopen("./user.txt","r");
fscanf(fp,"%s%s",s.name,s.pwd);
strcat(s.name,"\n");
strcat(s.pwd,"\n");
if(strcmp(p.name,s.name)==0
&&
strcmp(p.pwd,s.pwd)==0)
{
printf("登录成功!!!\n");
}
else
{
printf("登录失败!!!\n");
}
return 0;
}
#include"log.h"
int regist()
{
str s;
FILE *fp=fopen("./user.txt","w");
if(fp==NULL)
{
perror("fopen");
return -1;
}
printf("建立新账户:");
fgets(s.name,sizeof(s.name),stdin);
printf("建立新密码:");
fgets(s.pwd,sizeof(s.pwd),stdin);
fprintf(fp,"%s%s",s.name,s.pwd);
fclose(fp);
printf("注册成功!!!\n");
return 0;
}
#include"log.h"
int main()
{
int n;
while(1)
{
printf("1.注册账户\n");
printf("2.登录账户\n");
printf("3.退出\n");
printf("请选择:");
scanf("%d",&n);
getchar();
switch(n)
{
case 1:regist();break;
case 2:login();break;
case 3:exit(0);
}
}
return 0;
}
3,复制
#include<myhead.h>
int main(int argc, const char *argv[])
{
FILE *fp1=fopen("./1.txt","r");
FILE *fp2=fopen("./2.txt","w");
char str[5];
while(1)
{
fread(str,sizeof(str),1,fp1);
if(feof(fp1))
{
printf("读取成功!!!\n");
break;
}
else if(ferror(fp1))
{
printf("读取失败!!!\n");
break;
}
fwrite(str,sizeof(str),1,fp2);
}
fclose(fp1);
fclose(fp2);
fp2=fopen("./2.txt","r");
while(fgets(str,sizeof(str),fp2))
{
fputs(str,stdout);
}
fclose(fp2);
putchar(10);
return 0;
}