#include <stdio.h>
#include <string.h>
typedef struct usr{
char unm[21];
char pwd[21];
}user;
int main(int argc, const char *argv[])
{
FILE* userfile=fopen("./user_tible.txt","r");
printf("输入username:");
user u;
scanf("%s",u.unm);
printf("输入password:");
scanf("%s",u.pwd);
char name[21];
fgets(name,sizeof(name),userfile);
int len=strlen(name);
if(name[len-1]=='\n'){
name[len-1]='\0';
}
for(int i=0;i<strlen(name);i++){
if(name[i]!=u.unm[i]){
puts("用户名错误");
return -1;
}
}
char pwd[21];
fgets(pwd,sizeof(pwd),userfile);
int len2=strlen(pwd);
if(pwd[len2-1]=='\n'){
pwd[len2-1]='\0';
}
for(int i=0;i<strlen(pwd);i++){
if(pwd[i]!=u.pwd[i]){
puts("密码错误");
puts(pwd);
return -1;
}
}
puts("登录成功");
return 0;
}
作业:拷贝