简单的从txt文档中获取用户名和密码并验证
#include
#include
int recog_pwd(char name[20],char pwd[20]);
void main()
{
int isok;
char *name = "osd";
char *pwd = "123";
if((recog_pwd(name,pwd))==1)
{
printf("user and pwd right\n");
}else printf("user not exist or password is wrong\n");
}
int recog_pwd(char name[20],char pwd[20])
{
int s;
FILE *fp1;
char str[20];
/*
*以只读方式打开文件
*read.txt格式为 :用户名 密码 (中间有一空格)无限循环
*/
if ((fp1=fopen("read.txt","r"))==NULL)
{
printf("cannot open file\n");
return 0;
}
/*
*判断账户是否存在
*/
while(!feof(fp1))
{
fscanf(fp1,"%s",str);
if(strcmp(str,name)==0)
{
printf("the name :%s is ok\n",name);
fscanf(fp1,"%s",str);
if(strcmp(str,pwd)==0)
{
printf("the pwd :%s is ok\n",pwd);
return 1;
}else
{
printf("the pwd :%s is wrong\n",pwd);
fclose(fp1);
return 0;
}
}
fscanf(fp1,"%s",str);
}
fclose(fp1);
return 0;
}
附录:read.txt文本格式
qwe 345 asd 123 zxdc 4546 afgbc 123 deeerf 789we gggggg 00012