C语言从文本文档读取字符串(用户名和密码验证)

简单的从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

扫码关注公众号,回复关键字: cpp 即可获取源代码和测试数据以及其他更多C++ 代码资源,免费下载。

C语言从文本文档读取字符串(用户名和密码验证)_第1张图片

关注 工众号: 木石说  回复 “cpp”直接获取源代码下载链接。 

 

 

你可能感兴趣的:(C/C++)