数据结构 4.27

手动创建一个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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值