手动创建一个usr.txt文档,文档中存储用户的名字和密码,格式如下:
账户 密码,每个账户密码占一行:
zhangsan 12345
lisi abcd
要求:
实现登录功能。
终端输入账户密码
若账户密码均存在,则输出登录成功
若账户不存在,则输出账户不存在
若密码错误,则输出密码错误
#include <stdio.h>
#include <string.h>
int main(int argc,char * argv[])
{
char name[20],passwd[20];
printf("please input the name>");
scanf("%s",name);
printf("please input the passwd>");
scanf("%s",passwd);
FILE * fp = fopen("./usr.txt","r");
if(NULL == fp)
{
perror("fopen");
return -1;
}
char file_name[20],file_passwd[20];
while(1)
{
if(fscanf(fp,"%s %s",file_name,file_passwd) == EOF)
{
printf("usr error\n");
break;
}
if(strcmp(name,file_name) != 0)
continue;
if(strcmp(passwd,file_passwd) != 0)
printf("%s passwd error\n",name);
else
printf("%s login successfully\n",name);
break;
}
fclose(fp);
return 0;
}
linux@linux:~/IO/day2$ ./a.out
please input the name>lisi
please input the passwd>456
lisi passwd error
linux@linux:~/IO/day2$ ./a.out
please input the name>zhaoliu
please input the passwd>123
zhaoliu passwd error
linux@linux:~/IO/day2$ ./a.out
please input the name>shu
please input the passwd>1
usr error